Setting the correct selector when inserting objects into DOM with jQuery
<p>I am currently building a dynamic form that allows the user to add as many query strings as they would like.</p>

<p>In this I have to insert some logic that evaluates the selected operator from an options drop down control. If the user selects the Between value then I need to insert a second field into the current row so that there are two text input fields (aka To and From). Where I am struggling right now is on selecting elements as they are inserted in the DOM.  I'm new to jQuery and I thought I understood selectors but I am still missing something.</p>

<p>Code I have thus far </p>

<pre><code>    $(document).ready(function () {
                var index = 0;
                $('#add').click(function () {
                    InsertFields();
                });
                function InsertFields() {
                    index++;
                    $("&lt;div id='additionalfield'&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='" + index + "' 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');
                }
    //used live on the second buttons to rebind the function once the element was added.
                    $('#newAdd').live('click', function () {
                        index++;
                    $("&lt;div id='additionalfield'&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'&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='" + index + "' 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');
                });

                $('#btnRemove').live('click', function () {
                    $(this).parent().remove();
                });
    //first select if performed on first row works fine
                $('select[name=operator]').change(function () {
                    var foo = $('select[name=operator] option:selected').val();
                    alert(foo); //test to see if I can reach the value
                });
</code></pre>

<p><strong>updated</strong> based upon suggestion from Andrew</p>

<pre><code>$('select').live('change', '.operClass', function () {
            var foo2 = $('.operClass option:selected').val();
            alert(foo2);
        });
            });
</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>

<p>Another wierd thing I am seeing is once I create a row the previous row seems to loose the binding to the click event. i.e. if I have three rows rows 1 and 3 will fire but row 2 will not it is as if the .live() method only works on the last row created.</p>

<p>Is .live the wrong method to use here? I tried using .on() but that would never trigger anything. </p>

<p>I'd really appreicate any insight as to how to get the select controls to properly fire and return the correct selected value as well as if there are any suggetions on the best appraoch to rebind the control to an event when being inserted into the DOM.</p>

<p>thank you,</p>

<p><strong>Working Solution</strong>
Thanks to Andrew for getting me on the right track. The issue was related to not using unique identifiers within the DOM as well as my jQuery selector was off. Thanks to Andrews suggestions I was able to get the solution working. Below is the correct code:</p>

<pre><code>$(document).ready(function () {
            var index = 0;
            $('#add').click(function () {
                InsertFields();
            });
            function InsertFields() {
                index++;
                $("&lt;div id='additionalfield'&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='" + index + "' 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');
            }
            $('#newAdd').live('click', function () {
                index++;
                $("&lt;div id='additionalfield'&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='" + index + "' 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');
            });

            $('#btnRemove').live('click', function () {
                $(this).parent().remove();
            });

            $('.operClass').change(function () {
                var foo = $('.operClass option:selected').val();
                alert(foo); //test to see if I can reach the value
            });

            $('select').live('change', '.operClass', function () {
                var foo2 = $(this, '.operClass option:selected').val();
                alert(foo2);
            });
        });
</code></pre>
