Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:00:52+00:00 2026-05-26T20:00:52+00:00

I am currently building a dynamic form that allows the user to add as

  • 0

I am currently building a dynamic form that allows the user to add as many query strings as they would like.

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.

Code I have thus far

    $(document).ready(function () {
                var index = 0;
                $('#add').click(function () {
                    InsertFields();
                });
                function InsertFields() {
                    index++;
                    $("<div id='additionalfield'><select name='condition'><option value='AND'>AND</option><option value='OR'>OR</option></select><input type='text' name='fieldname' id='fieldName' size='20' /><select name='operator'><option id='opt1' value='contains'>Contains</option><option id='opt2' value='does not contain'>Does Not Contain</option><option id='opt3' value='like'>Like</option><option id='opt4' value='between'>Between</option></select><input type='text' name='fieldValue1' id='fieldvalue1' value='" + index + "' size='20' /> <input type='button' id='newAdd' value='Add' /> <input type='button' id='btnRemove' value='Remove' /></div>").appendTo('#queryFields');
                }
    //used live on the second buttons to rebind the function once the element was added.
                    $('#newAdd').live('click', function () {
                        index++;
                    $("<div id='additionalfield'><select name='condition'><option value='AND'>AND</option><option value='OR'>OR</option></select><input type='text' name='fieldname' id='fieldName' size='20' /><select id='newOperator' name='operator'><option id='opt1' value='contains'>Contains</option><option id='opt2' value='does not contain'>Does Not Contain</option><option id='opt3' value='like'>Like</option><option id='opt4' value='between'>Between</option></select><input type='text' name='fieldValue1' id='fieldvalue1' value='" + index + "' size='20' /> <input type='button' id='newAdd' value='Add' /> <input type='button' id='btnRemove' value='Remove' /></div>").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
                });

updated based upon suggestion from Andrew

$('select').live('change', '.operClass', function () {
            var foo2 = $('.operClass option:selected').val();
            alert(foo2);
        });
            });

html

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

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.

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

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.

thank you,

Working Solution
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:

$(document).ready(function () {
            var index = 0;
            $('#add').click(function () {
                InsertFields();
            });
            function InsertFields() {
                index++;
                $("<div id='additionalfield'><select name='condition'><option value='AND'>AND</option><option value='OR'>OR</option></select><input type='text' name='fieldname' id='fieldName' size='20' /><select name='operator'class='operClass'><option id='opt1' value='CONTAINS'>Contains</option><option id='opt2' value='DOES NOT CONTAIN'>Does Not Contain</option><option id='opt3' value='LIKE'>Like</option><option id='opt4' value='BETWEEN'>Between</option></select><input type='text' name='fieldValue1' id='fieldvalue1' value='" + index + "' size='20' /> <input type='button' id='newAdd' value='Add' /> <input type='button' id='btnRemove' value='Remove' /></div>").appendTo('#queryFields');
            }
            $('#newAdd').live('click', function () {
                index++;
                $("<div id='additionalfield'><select name='condition'><option value='AND'>AND</option><option value='OR'>OR</option></select><input type='text' name='fieldname' id='fieldName' size='20' /><select id='newOperator' name='operator' class='operClass'><option id='opt1' value='CONTAINS'>Contains</option><option id='opt2' value='DOES NOT CONTAIN'>Does Not Contain</option><option id='opt3' value='LIKE'>Like</option><option id='opt4' value='BETWEEN'>Between</option></select><input type='text' name='fieldValue1' id='fieldvalue1' value='" + index + "' size='20' /> <input type='button' id='newAdd' value='Add' /> <input type='button' id='btnRemove' value='Remove' /></div>").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);
            });
        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:00:52+00:00Added an answer on May 26, 2026 at 8:00 pm

    The id attribute must be unique in the DOM, that is why all your calls are behaving strangely, use a unique id for each element and group like elements by adding classes, your listeners will then become,

    $(document).on('click', '.classOfElement', function(){...});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a dynamic form creator that allows users to add form elements
I am currently building a dynamic url tab system that user can say what
I'm currently building a Java app that could end up being run on many
I'm currently building a project and I would like to make use of some
I am currently building a very dynamic table for a list application, which will
We're currently building an application that executes a number of external tools. We often
I'm currently building a small web application that includes a fair amount of JavaScript.
I am currently building an Excel 2007 Add-in using VSTO (latest version + sp1)
I currently have a db structure that looks something like what i have below
I'm currently building a dynamic website based on jQuery en hashChanged. Currently I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.