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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:16+00:00 2026-06-13T22:57:16+00:00

I have two selects that are linked: Each value of the first select determines

  • 0

I have two selects that are linked: Each value of the first select determines which items will be displayed in the second select.

The values of the second select are stored in a two-dimension array:

[ [{"id":1,"text":"a"}, {"id":2,"text":"b"},...],
  [{"id":"1a","text":"aa"},{"id":"1b","text":"ba"},...],
  ...
]

The first select value determines the index to be used to populate the second select. So in a ‘change’ event on the first I should be able to modify the items select-two contains.

Reading documentation I think I need to use the “data” option… but not shure how as the example loads the array data on initialization and it seems to don’t work if I try to do the same after initialization.

HTML

Attribute:
<select name="attribute" id="attribute">
    <option value="0">Color</option>
    <option value="1">Size</option>
</select>

Value:
<select name="value" id="value"></select>

<script>
   var data = [ [{"id":1,"text":"black"}, {"id":2,"text":"blue"},...],
                [{"id":"1","text":"9"},{"id":"1","text":"10"},...],
              ];
   $('#attribute').select2().bind('change', function(){
      // Here I need to change `#value` items.
      $('#value').select2('data',data[$(this).val()]);  // This does not work
   );

   $('#value').select2();
</script>
  • 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-13T22:57:17+00:00Added an answer on June 13, 2026 at 10:57 pm

    I’ve made an example for you showing how this could be done.

    Notice the js but also that I changed #value into an input element

    <input id="value" type="hidden" style="width:300px"/>
    

    and that I am triggering the change event for getting the initial values

    $('#attribute').select2().on('change', function() {
        $('#value').select2({data:data[$(this).val()]});
    }).trigger('change');
    

    Code Example

    Edit:

    In the current version of select2 the class attribute is being transferred from the hidden input into the root element created by select2, even the select2-offscreen class which positions the element way outside the page limits.

    To fix this problem all that’s needed is to add removeClass('select2-offscreen') before applying select2 a second time on the same element.

    $('#attribute').select2().on('change', function() {
        $('#value').removeClass('select2-offscreen').select2({data:data[$(this).val()]});
    }).trigger('change');
    

    I’ve added a new Code Example to address this issue.

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

Sidebar

Related Questions

I have two tables that both have an ID number which are linked together
I have two HTML select boxes. The first is called #1 and the second
I have a series of two chained selects using ajax that work great. I
I have two select menus (#id1 and #id2) that, when validated as containing a
I have two tables that I am joining with the following query... select *
I currently have a ajax script that dynamically builds two select boxes enabled with
I have two forms, one with a radio button that users must select to
I have two simple models: Book, and Author Each Book has one Author, linked
I currently have two tables similar to users and programs that are linked through
I have a two ibatis sql maps that are linked together via a sub

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.