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 746593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:06:31+00:00 2026-05-14T14:06:31+00:00

Basically what I have here is a button that adds a new list item

  • 0

Basically what I have here is a button that adds a new list item to an unordered list. Within this list item is a dropdown box (.formSelector) that when changed triggers an ajax call to get the values for another dropdown box (.fieldSelector) in the same list item.

The problem is on this line:

$(this).closest(".fieldSelector").append(

If I change it to

$(".fieldSelector").append(

the second box updates correctly. The problem with this is that when I have multiple list items it updates every dropdown box on the page with the class fieldSelector, which is undesirable. How can I traverse the DOM to just pick the fieldSelector in the current list item? Any help is greatly appreciated.

Complete code:

$("button", "#newReportElement").click(function() { 
        $("#reportElements").append("<li class=\"ui-state-default\"> <span class=\"test ui-icon ui-icon-arrowthick-2-n-s \"></span><span class=\"ui-icon ui-icon-trash deleteRange \"></span> <select name=\"form[]\" class=\"formSelector\"><? foreach ($forms->result() as $row) { echo "<option value='$row->formId'>$row->name</option>"; }?></select><select class=\"fieldSelector\" name=\"field[]\"></select></li>");                                   
        $(".deleteRange").button(); 
        $('.formSelector').change(function() { 
            var id = $(this).val();     
            var url = "<? echo site_url("report/formfields");?>" + "/" + id;   
            var atext = "ids:"; 

            $.getJSON(url,
                function(data){
                  $.each(data.items, function(i,item){

                    $(this).closest(".fieldSelector").append(
                            $('<option></option>').val(item.formId).html(item.formLabel)
                        );
                  });
                });    

        });
        return false; 
    });
  • 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-14T14:06:31+00:00Added an answer on May 14, 2026 at 2:06 pm

    this has been lost in a barrage of callback functions. Inside your change() handler, store a reference to the list element that originally initiated the event, then later reference it making use of closures.

    Edit:
    .fieldSelector and .formSelector are sibings and not parent/child. Since closest starts looking upwards in the hierarchy, it won’t find anything. Instead use the siblings method.

    $('.formSelector').change(function() {
        var listItem = $(this).siblings(".fieldSelector");
        ...
        $.getJSON(...
            $.each(...
                listItem.append(...);
            );
        });
    });    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing basically the same issue described here I have a table that starts
Okay basically here's where I'm at. I have a list of PropertyDescriptor objects. These
I have a class that basically stores files in amazon s3. Here is what
Here's the problem: I have a data-bound list of items, basically a way for
I am stuck with a peculiar issue here. I have a script that basically
Basically I have a webcontrol that contains a gridview with an export button. When
I have an example here . Basically i have a scroll bar that is
I have following components - ColorButton, that represents single button that is basically colored
I have a button that i basically want to either show or hide it
I have an edit page with the following code, basically a dropdown and button

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.