how to get the count of elements within a dynamically generated DOM object
<p>within a form I have a section that allows the user to build out a search query. Within the options the user has the ability to select different operators ( contains, does not contain, like, between).</p>

<p>When the user selects between I insert a new input field after the drop down menu which would server as a to from field see image:
<img src="http://i.stack.imgur.com/U6eWX.png" alt="enter image description here"></p>

<p>The issue I am having is how I am testing for the existence of the field. Currently I am doing this  based upon the value of the object.  This approach does not work if the field was not originally set to between.  I.E if it is on contains and I select like the field is removed. </p>

<p>I think I need to  count the number of text input fields in the current div and then if the number is greater than 2 remove one otherwise do nothing. </p>

<p>Where I am stuck is in the ability to count the input elements for the current div </p>

<p>I have tried:</p>

<pre><code>var cnt = $(this, 'div .operClass input:text').length;
</code></pre>

<p>but this counts all inputs in the form vs the current row. </p>

<p>Can anyone point me in the right direction on when an option is selected to count and return the number of input fields for the current div that the selector is inside?</p>

<p>Thanks in advance,</p>

<p>jscript </p>

<pre><code>$(document).ready(function () {
            $('#add').click(function () {
                InsertFields();
            });
            //insert first row
            function InsertFields() {
                $("&lt;div class='additionalrow'&gt;&lt;select name='condition'&gt;&lt;option value='AND'&gt;AND&lt;/option&gt;&lt;option value='OR'&gt;OR&lt;/option&gt;&lt;/select&gt;&lt;input type='text' name='fieldname' id='fieldName' size='20' /&gt;&lt;select name='operator'class='operClass'&gt;&lt;option id='opt1' value='CONTAINS'&gt;Contains&lt;/option&gt;&lt;option id='opt2' value='DOES NOT CONTAIN'&gt;Does Not Contain&lt;/option&gt;&lt;option id='opt3' value='LIKE'&gt;Like&lt;/option&gt;&lt;option id='opt4' value='BETWEEN'&gt;Between&lt;/option&gt;&lt;/select&gt;&lt;input type='text' name='fieldValue1' id='fieldvalue1' value='' size='20' /&gt; &lt;input type='button' id='newAdd' value='Add' /&gt; &lt;input type='button' id='btnRemove' value='Remove' /&gt;&lt;/div&gt;").appendTo('#queryFields');
            }
            //add additional row 
            //used second funciton to reset bindings
            $('#newAdd').live('click', function () {
                $("&lt;div class='additionalrow'&gt;&lt;select name='condition'&gt;&lt;option value='AND'&gt;AND&lt;/option&gt;&lt;option value='OR'&gt;OR&lt;/option&gt;&lt;/select&gt;&lt;input type='text' name='fieldname' id='fieldName' size='20' /&gt;&lt;select id='newOperator' name='operator' class='operClass'&gt;&lt;option id='opt1' value='CONTAINS'&gt;Contains&lt;/option&gt;&lt;option id='opt2' value='DOES NOT CONTAIN'&gt;Does Not Contain&lt;/option&gt;&lt;option id='opt3' value='LIKE'&gt;Like&lt;/option&gt;&lt;option id='opt4' value='BETWEEN'&gt;Between&lt;/option&gt;&lt;/select&gt;&lt;input type='text' name='fieldValue1' id='fieldvalue1' value='' size='20' /&gt; &lt;input type='button' id='newAdd' value='Add' /&gt; &lt;input type='button' id='btnRemove' value='Remove' /&gt;&lt;/div&gt;").appendTo('#queryFields');
            });
            //remove row
            $('#btnRemove').live('click', function () {
                $(this).parent().remove();
            });

            $('.operClass').change(function () {
                var foo = $('.operClass option:selected').val();
                alert(foo); 
            });

            $('select').live('change', '.operClass', function () {
                var sb = $(this, '.operClass option:selected').val();
                var ci = $(this, '.operClass');
                insertFromField(sb, ci);
            });
            //insert field after between option or remove it if it exists
            function insertFromField(param1, param2, param3) {

                if (param1 == "BETWEEN") {
                    $("&lt;input type='text' name='fieldValue1' id='fieldvalue1' value='' size='20' /&gt;").insertAfter(param2);    
                }
                else {
                //need to test count of existing input fields for the current row here if n &gt; 1 remove the second field else do nothing.
                    $(param2).next().remove();
                }
            }
        });  
</code></pre>

<p>html</p>

<pre><code>&lt;div id="queryFields"&gt;
            &lt;select name="condition"&gt;
                &lt;option value="AND"&gt;AND&lt;/option&gt;
                &lt;option value="OR"&gt;OR&lt;/option&gt;
            &lt;/select&gt;
            &lt;input type="text" name="fieldname" id="fieldName" size="20" /&gt;
            &lt;select  name="operator"&gt;
                &lt;option id="opt1" value="CONTAINS"&gt;Contains&lt;/option&gt;
                &lt;option id="opt2" value="DOES NOT CONTAIN"&gt;Does Not Contain&lt;/option&gt;
                &lt;option id="opt3" value="LIKE"&gt;Like&lt;/option&gt;
                &lt;option id="opt4" value="BETWEEN"&gt;Between&lt;/option&gt;
            &lt;/select&gt;
            &lt;input type="text" name="fieldValue1" id="fieldvalue1" value="" size="20" /&gt;
            &lt;input type="button" id="add" value="Add" /&gt; 
        &lt;/div&gt;
</code></pre>
