Counting the number of dynamically added divs in jquery
<p>I'm new to JQuery so I'm possibly doing this completely wrong. I want to count the number of divs there are within another div as I dynamically add to them. There are already several divs there already. Basically what I do is click a button, it adds a new div and alerts the total number of divs, the old plus the new. This is what I have so far and it's not working.
Here's the Jquery:</p>

<pre><code>&lt;!-- Jquery to Add New Textarea --&gt;

&lt;script type="text/javascript"&gt;

    $(document).ready(function(){

        $('#addTertButton').click(function(){

            //Shows 
            alert($('div.addTertDiv').size());

            $('.addTertDiv').append("&lt;?php echo $tertDiv; ?&gt;");

        });
    });
&lt;/script&gt;
</code></pre>

<p>And here is the php:</p>

<pre><code>                    &lt;div class="docedcont"&gt;
                        &lt;div class="tertiarywrapper"&gt;
                            &lt;?php

                                $counttert = count($this_rds-&gt;tertiary_array);
                                for($i = 0; $i &lt; $counttert; ++$i) {
                                    echo("&lt;div id='tertiaryselect'&gt;");


                                        echo("&lt;br /&gt;".$this_rds-&gt;rds_number.".".$this_rds-&gt;physreq_number.".".$this_rds-&gt;tertiary_array[$i]['tertiary_number']." ".$this_rds-&gt;tertiary_array[$i]['tertiary_title']."&lt;br /&gt;&lt;br /&gt;");

                                        echo("&lt;textarea name=\"tertiary_text-".$i."\" cols=\"50\" rows=\"10\" class=\"dark\" onfocus=\"this.className='light'\" onblur=\"this.className='dark'\"&gt;");

                                            echo $this_rds-&gt;tertiary_array[$i]['tertiary_text'];

                                        echo("&lt;/textarea&gt;");

                                        echo("&lt;input name=\"tertiary_id-".$i."\" type=\"hidden\" value=\"".$this_rds-&gt;tertiary_array[$i]['tertiary_id']."\"&gt;");
                                        echo("&lt;input name=\"tertiary_type-".$i."\" type=\"hidden\" value=\"gb,ie,in,ae\"&gt;");
                                        echo("&lt;input name=\"tertiary_number-".$i."\" type=\"hidden\" value=\"".$this_rds-&gt;tertiary_array[$i]['tertiary_number']."\"&gt; ");
                                        echo("&lt;input name=\"tertiary_physreqlink_id-".$i."\" type=\"hidden\" value=\"".$this_rds-&gt;tertiary_array[$i]['tertiary_physreqlink_id']."\"&gt;");
                                        echo("&lt;input name=\"tertiary_modby-".$i."\" type=\"hidden\" value=\"".$this_rds-&gt;tertiary_array[$i]['tertiary_modby']."\"&gt;");
                                        echo("&lt;input name=\"tertiary_moddate-".$i."\" type=\"hidden\" value=\"".$this_rds-&gt;tertiary_array[$i]['tertiary_moddate']."\"&gt;");

                                        //Subsection Publish Checkbox
                                        echo("Publish Subsection");
                                        if ($this_rds-&gt;tertiary_array[$i]['tertiary_show']=="yes"){
                                            echo("&lt;input name=\"tertiary_show-".$i."\" type=\"checkbox\" value=\"yes\" checked /&gt;");
                                        }
                                        else {
                                            echo("&lt;input name=\"tertiary_show-".$i."\" type=\"checkbox\" value=\"no\" /&gt;");
                                        }

                                        echo("&lt;input name=\"counttert\" type=\"hidden\" value=\"". $counttert."\"&gt;");

                                    echo("&lt;/div&gt;"); 

                                }
                                    echo("&lt;div class=\"addTertDiv\"&gt;&lt;/div&gt;");

                            ?&gt;
                        &lt;/div&gt;
                    &lt;?php                                                       
                        //Add New Subsection
                        //On button click create new textarea and increase "counttert"
                     echo ("&lt;button type=\"button\" id=\"addTertButton\" value=\"".$counttert."\"&gt;Click Me!&lt;/button&gt;");
                     ?&gt; 
                    &lt;/div&gt;
</code></pre>

<p>Any help would be greatly appreciated.</p>
