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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:53:50+00:00 2026-05-10T17:53:50+00:00

Ok, here’s one for the JavaScript gurus: In my app, one of the controllers

  • 0

Ok, here’s one for the JavaScript gurus:

In my app, one of the controllers passes a TreeMap to it’s JSP. This map has car manufacturer’s names as keys and Lists of Car objects as values. These Car objects are simple beans containing the car’s name, id, year of production etc. So, the map looks something like this (this is just an example, to clarify things a bit):

Key: Porsche
Value: List containing three Car objects(for example 911,Carrera,Boxter with their respectable years of production and ids)
Key: Fiat
Value: List containing two Car objects(for example, Punto and Uno)
etc…

Now, in my JSP i have two comboboxes. One should receive a list of car manufacturers(keys from the map – this part I know how to do), and the other one should dynamicaly change to display the names of the cars when the user selects a certain manufacturer from the first combobox. So, for example, user selects a "Porsche" in the first combobox, and the second immediately displays "911, Carrera, Boxter"…

After spending a couple of days trying to find out how to do this, I’m ready to admit defeat. I tried out a lot of different things but every time I hit a wall somewehere along the way. Can anybody suggest how I should approach this one? Yes, I’m a JavaScript newbie, if anybody was wondering…

EDIT: I’ve retagged this as a code-challenge. Kudos to anybody who solves this one without using any JavaScript framework (like JQuery).

  • 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. 2026-05-10T17:53:51+00:00Added an answer on May 10, 2026 at 5:53 pm

    Well anyway, as i said, i finally managed to do it by myself, so here’s my answer…

    I receive the map from my controller like this (I’m using Spring, don’t know how this works with other frameworks):

    <c:set var='manufacturersAndModels' scope='page' value='${MANUFACTURERS_AND_MODELS_MAP}'/> 

    These are my combos:

    <select id='manufacturersList' name='manufacturersList' onchange='populateModelsCombo(this.options[this.selectedIndex].index);' >                   <c:forEach var='manufacturersItem' items='<%= manufacturers%>'>                     <option value='<c:out value='${manufacturersItem}' />'><c:out value='${manufacturersItem}' /></option>                   </c:forEach>                 </select> 

    select id='modelsList' name='modelsList'                   <c:forEach var='model' items='<%= models %>' >                     <option value='<c:out value='${model}' />'><c:out value='${model}' /></option>                   </c:forEach>                 </select> 

    I imported the following classes (some names have, of course, been changed):

    <%@ page import='org.mycompany.Car,java.util.Map,java.util.TreeMap,java.util.List,java.util.ArrayList,java.util.Set,java.util.Iterator;' %> 

    And here’s the code that does all the hard work:

    <script type='text/javascript'> <%        Map mansAndModels = new TreeMap();      mansAndModels = (TreeMap) pageContext.getAttribute('manufacturersAndModels');      Set manufacturers = mansAndModels.keySet(); //We'll use this one to populate the first combo      Object[] manufacturersArray = manufacturers.toArray();       List cars;      List models = new ArrayList(); //We'll populate the second combo the first time the page is displayed with this list    //initial second combo population      cars = (List) mansAndModels.get(manufacturersArray[0]);       for(Iterator iter = cars.iterator(); iter.hasNext();) {         Car car = (Car) iter.next();        models.add(car.getModel());      } %>   function populateModelsCombo(key) {   var modelsArray = new Array();    //Here goes the tricky part, we populate a two-dimensional javascript array with values from the map <%                                for(int i = 0; i < manufacturersArray.length; i++) {         cars = (List) mansAndModels.get(manufacturersArray[i]);        Iterator carsIterator = cars.iterator();            %>     singleManufacturerModelsArray = new Array(); <%     for(int j = 0; carsIterator.hasNext(); j++) {        Car car = (Car) carsIterator.next();   %>              singleManufacturerModelsArray[<%= j%>] = '<%= car.getModel()%>';  <%        }  %>   modelsArray[<%= i%>] = singleManufacturerModelsArray;  <%      }           %>       var modelsList = document.getElementById('modelsList');    //Empty the second combo   while(modelsList.hasChildNodes()) {     modelsList.removeChild(modelsList.childNodes[0]);   }   //Populate the second combo with new values   for (i = 0; i < modelsArray[key].length; i++) {      modelsList.options[i] = new Option(modelsArray[key][i], modelsArray[key][i]);   }       } 

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Here's an example from a project I am currently working… May 11, 2026 at 2:42 pm
  • added an answer My feeling would be something like class CItem(list): def __eq__(self,… May 11, 2026 at 2:42 pm
  • added an answer If there are any records for which Category is dbnull,… May 11, 2026 at 2:42 pm

Related Questions

Ok, here's the breakdown of my project: I have a web project with a
Ok, here's one for the JavaScript gurus: In my app, one of the controllers
Ok, I know someone here has tried this ninja-elite level of coding before. Essentially
Ok guys and gals, here is my problem: I've built a custom control that
OK, Here is my problem, I have a master page with a HEAD section

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.