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

  • SEARCH
  • Home
  • 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 3281642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:50:17+00:00 2026-05-17T19:50:17+00:00

Ok this might be a bit confusing, but here goes. Let’s say I have

  • 0

Ok this might be a bit confusing, but here goes. Let’s say I have a a few select dropdowns on a page.

<select id="filter" name="filter[]">
        <option value="">-- Select Filter --</option>
    </select>

    <select id="load_choice" name="load_choice[]">
        <option value="">-- Select Load_choice --</option>
    </select>

    <select id="plastic" name="plastic[]">
        <option value="">-- Select Plastic --</option>
    </select>

These are dynamically filled from a database with an ajax request. Each set of select options are dependent on the previous selection. This is just a snippet of all the select dropdowns, but essentially their selections creates a “product”. Here is the javascript that connects to the php (which connects to the DB).

$(document).ready(function() {
$('#filter').change(function(){

        $('#load_choice').fadeOut();
        $('#loader').show();

        $.post("ajax/ajax_load_choice.php", {
            country: $('#country').val(),
            filter: $('#filter').val()
        }, function(response){
            setTimeout("finishAjax('load_choice', '"+escape(response)+"')", 400);
        });
        return false;
    });


    $('#load_choice').change(function(){

        $('#plastic').fadeOut();
        $('#loader').show();

        $.post("ajax/ajax_plastic.php", {
            country: $('#country').val(),
            filter: $('#filter').val(),
            load_choice: $('#load_choice').val()
        }, function(response){
            setTimeout("finishAjax('plastic', '"+escape(response)+"')", 400);
        });
        return false;
    });


    $('#plastic').change(function(){

        $('#UG_tipping').fadeOut();
        $('#loader').show();

        $.post("ajax/ajax_UG.php", {
            country: $('#country').val(),
            filter: $('#filter').val(),
            load_choice: $('#load_choice').val(),
            plastic: $('#plastic').val()
        }, function(response){
            setTimeout("finishAjax('UG_tipping', '"+escape(response)+"')", 400);
        });
        return false;
    });

    });

    function finishAjax(id, response){
     $('#loader').hide();
     $('#'+id).html(unescape(response));
     $('#'+id).fadeIn();
    }
}

NOW, let’s say I want to add another of the exact same form with the exact same select options in order to “create another product” on the same page (hence the array on the NAME tag for each select). Since the form is dependent on unique IDs, how could I make the IDs in this chunk of code dynamic?

$('#filter').change(function(){

        $('#load_choice').fadeOut();
        $('#loader').show();

        $.post("ajax/ajax_load_choice.php", {
            country: $('#country').val(),
            filter: $('#filter').val()
        }, function(response){
            setTimeout("finishAjax('load_choice', '"+escape(response)+"')", 400);
        });
        return false;
    });

I will eventually have 5 sets of those select groups so the user can create 5 products. How would I make it so I don’t have to do id=”filter1″ and id=”filter2″ to coincide with $(‘#filter’).change(function….blah blah blah. Basically, can I make ID’s in a jquery function dynamic?

  • 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-17T19:50:17+00:00Added an answer on May 17, 2026 at 7:50 pm

    I’m not, really, sure what you’re asking. My best guess is something along the lines of:

    Using jQuery, can I increment the id fields of the form elements in order that I can add multiple forms to the page, and have each form uniquely identified?

    If this is a reasonable understanding of your question, and looking at @Baylor Rae’s answer (which is definitely a valid interpretation) I’m not necessarily right, then the following should work:

    jQuery

    $(document).ready(
     function(){
      $('#formAdd').click(
          function(){
              var count = $('#formsBox form').length;
              $('#formsBox form').eq(0).clone().appendTo('#formsBox').find('label, input').each(
                  function(){
                      if (this.id) {
                          var curId = this.id;
                          $(this).attr('id',curId + count);
                      }
                      else if ($(this).attr('for')) {
                          var curFor = $(this).attr('for');
                          $(this).attr('for', curFor + count);
                      }
                  }
                  );
          }
          );
     }
     );
    

    html

    <div id="formsBox">
        <form action="" method="post">
            <fieldset>
                <ul>
                    <li>
                        <label for="firstInput">First Input:</label>
                        <input type="text" name="firstInput" id="firstInput" />
                    </li>
                    <li>
                        <label for="secondInput">SecondInput:</label>
                        <input type="text" name="secondInput" id="secondInput" />
                    </li>
                </ul>
            </fieldset>
        </form>
    </div>
    <div id="formAdd">Add another form</div>
    

    Effectively, on clicking the #formAdd div the jQuery finds the form, works out how many there are and stores that as the variable count.

    It then clones the first form (without the .eq(0) the first click clones one form, the second click clones two forms and it’s exponential thereafter), appends it to the same container (#formsBox, for want of a better name) and then looks for the labels and inputs; if these have an id (input) or a for (label) attribute, it adds the previously-stored count variable to these attributes, uniquely identifying them.

    This is a pretty poor explanation, and I’m sure there’s a far prettier way of achieving the same end-result in the jQuery, but it does seem to work. There’s a JS Fiddle demo here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a bit of a silly question but; If I have two
Title might be a bit confusing, so let me explain. I have a website
OK, this might sound a bit confusing and complicated, so bear with me. We've
This might be a bit on the silly side of things but I need
This might sound like a little bit of a crazy question, but how can
This might be a bit difficult to explain in writing, so please bear with
This might seem like a stupid question I admit. But I'm in a small
This might be on the discussy side, but I would really like to hear
This might seem obvious but I've had this error when trying to use LINQ
This might be an odd question, but when I scale my image in C#

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.