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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:31:35+00:00 2026-05-27T22:31:35+00:00

I am having difficulty traversing with jQuery. I have a form with a dynamic

  • 0

I am having difficulty traversing with jQuery. I have a form with a dynamic set of dropdown boxes, and am trying to call particular actions based on element class instead of id, as this form can be cloned in my application.

Any ideas why my traversing isn’t working? Is my $(this) call becoming voided somehow?

HTML:

<div>
    <label>Item:</label>
    <select class="item" name="item" id="item"> 
        <option value="">Select</option>
        ...
    </select>
</div>          
<div>
    <label class="label>Options:</label>
    <select class="options" name="options" id="options">

        ...
    /select>
</div>          

Javascript:

$(".item").change(function () { 

  var group_id = $(this).val();

   $.ajax({
        type: "POST", 
        url: "../../db/items.php?id=" + group_id, 
        dataType: "json",
        success: function(data){
            $(this).parent().next().children('select.options').empty(); 
            $(this).parent().next().children('select.options').append('<option value="">Select</option>');
            $.each(data, function(i, val){    
               $(this).parent().next().children('select.options').append('<option value="' + val.group_id + '">' + val.name + '</option>');
            });
            $(this).parent().next().children('select.options').focus();
        },
        beforeSend: function(){
            $(this).parent().next().children('select.options').empty();
            $(this).parent().next().children('select.options').append('<option value="">Loading...</option>');
        },
        error: function(){
            $(this).parent().next().children('select.options').attr('disabled', true);
            $(this).parent().next().children('select.options').empty();
            $(this).parent().next().children('select.options').append('<option value="">No Options</option>');
        }
    })  

});
  • 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-27T22:31:36+00:00Added an answer on May 27, 2026 at 10:31 pm

    this is no longer the DOM element that the event was triggered on. You can set the value of this by using the context option of $.ajax:

    $(".item").change(function () {
        $.ajax({
            /* options */
            context: this,
            success: function () {
                // this is now the element that was changed
            }
        });
    });
    

    You could also store the value of this in a variable and use it in your callback:

    $(".item").change(function () { 
    
      var group_id = $(this).val();
      var self = this;
    
       $.ajax({
            type: "POST", 
            url: "../../db/items.php?id=" + group_id, 
            dataType: "json",
            success: function(data){
                // use "self" instead of "this"
            }
        })  
    
    });
    

    Another note: You should consider caching your selectors for greater performance (and readability). Also, you can chain together jQuery function calls:

    var $children = $(this).parent().next().children('select.options')
        .empty()
        .append('<option value="">Select</option>');
    
    $.each(data, function(i, val){    
       $children.append('<option value="' + val.group_id + '">' + val.name + '</option>');
    });
    $children.focus();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having difficulty calling a function after a jQuery AJAX call has finished
I'm having difficulty trying to remove a div with a particular ID, and its
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I'm having difficulty using MooseX::Declare properly when calling BUILDARGS. I'm trying to create an
I'm having difficulty implementing an accordion effect on three different tables using jQuery and
Having difficulty making my javascript act the way I want. I have a button,
I'm having difficulty translating sql to linq syntax. I have 2 tables (Category and
I'm having difficulty figuring out how to have StructureMap cache a non-default instance. I
I am having difficulty figuring out how to number my subreport's records. I have

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.