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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:42:06+00:00 2026-06-14T19:42:06+00:00

i have 4 select lists. Content of the first one loading on page load.

  • 0

i have 4 select lists. Content of the first one loading on page load. The contents of each other depends on the selected item in the previous list.

I have one function, which loads the data from the url address in the list. This works fine when the user selects a value by one in each list.
Sometimes it is necessary to set all the values ​​from the code and it is a problem.

Now, i have function like this:

var c = $('#container');
var f1 = c.find('.myval1').val();
var f2 = c.find('.myval2').val();
var f3 = c.find('.myval3').val();
var f4 = c.find('.myval4').val();

if(f1.length > 0){
c.find('.form').find('.s1').children('option').filter(function() {
   return $(this).text().toLowerCase() == f1.toLowerCase();
}).attr('selected', 'selected');
parent.find('.search-form').find('.s1').change();
}

if(f2.length > 0){
c.find('.form').find('.s2').children('option').filter(function() {
   return $(this).text().toLowerCase() == f2.toLowerCase();
}).attr('selected', 'selected');
parent.find('.form').find('.s2').change();
}

if(f3.length > 0){
c.find('.form').find('.s3').children('option').filter(function() {
   return $(this).text().toLowerCase() == f3.toLowerCase();
}).attr('selected', 'selected');
parent.find('.form').find('.s3').change();
}

if(f4.length > 0){
c.find('.form').find('.s4').children('option').filter(function() {
   return $(this).text().toLowerCase() == f4.toLowerCase();
}).attr('selected', 'selected');
parent.find('.form').find('.s4').change();
}

s1,s2,s3,s4 are select lists

The problem is that when we need to select an item from the second list, it is still empty, because Ajax function has not yet done.

The main question that must wait for the event change() done, which loads the content in select before we filter content in it

I know about $.Deferred() but do not know how to apply it to this code.

  • 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-06-14T19:42:07+00:00Added an answer on June 14, 2026 at 7:42 pm

    First I think that myval1, myval2, … should be ids instead of classes (since it seems to uniquely identify an element) and second, you should refactor your code in order to improve readability since you make the same thing for your 4 elements :

    var c = $('#container');
    
    function init() {
    
        for(var i = 0; i < 4; i++) {
            updateList(c, c.find('#myval' + i).val(), i);
    
            // I guess it is here that you have to update your lists
            bindChange(i);
        }
    }
    
    function updateList(c, f, i) {
        if(f.length > 0) {
            c.find('.form').find('.s' + i).children('option').filter(function() {
                return $(this).text().toLowerCase() == f.toLowerCase();
            }).attr('selected', 'selected');
    
            // -> Here, is parent really different from c? And for the 1st element, 
            // is it filtered by the class .search-form instead of .form?
            parent.find('.form').find('.s' + i).change();
        }
    }
    
    
    // New functions in order to trigger an update of the lists
    // and to update subsequent list
    function bindChange(i) {
        parent.find('.form').find('.s' + i).on('change', updateContent(i));
    }
    
    function updateContent(i) {
        return function() {
            $.ajax({
                url: 'yoursite.html',
                data: yourdata,
                success : updateSubsequentList(i)
            });
        }
    }
    
    function updateSubsequentList(i) {
        return function() {
            var j = i + 1;
            updateList(c, c.find('#myval' + j).val(), j);
        }
    }
    

    Here I give index to the different functions, but you can get the object on which we are currently working on by using keyword this directly…

    So, to summarize, you should use a callback function in your ajax function inside the key success.

    Hope this will help you…

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

Sidebar

Related Questions

I have a table with many rows and each row has select lists. Something
I have dropdown-Lists which have a lot of elements (>1000). When I select one
I have this dropdown. I need clone for my other dropdown lists: <select name=general>
I have two select lists, I would like jquery to either remove or disable
Currently we have dueling select lists where you can select multiple employees from Available
I have implenmented drop-down lists Select menus in jQuery Mobile. <div data-role=fieldcontain> <label for=name
i have this lists, and i want select li element from jquery when the
I have a dropdownlist which lists Country names When user select any country from
I have a select list. I want to append a new item in italic
I have a gsp page and on that have a ' <g:select> ' element

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.