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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:40:06+00:00 2026-05-20T03:40:06+00:00

I have a map object I am placing in a Spring ModelAndView in my

  • 0

I have a map object I am placing in a Spring ModelAndView in my controller and forwarding to my jsp view to populate a select. After it populates the first time, I want to replace the map object used to populate the select with a json object I am retrieving using jquery AJAX and converting to an object using jQuery.parseJSON. Can I dynamically replace the entire contents of the select with the contents of the json object?

  • 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-20T03:40:07+00:00Added an answer on May 20, 2026 at 3:40 am

    For actually modifying the options, you don’t really need jQuery. You can clear the old options by assigning to the length property of the options property of the SELECT box, and then add new options via #add and new Option().

    Here are a couple of examples using jQuery for the XHR part and then doing the options directly:

    From an array:

    If you’re drawing the data from an array within the object (in this case, an array identified by the property options on the resulting object):

    JSON:

    {
      "options": [
        {"value": "New1", "text": "New Option 1"},
        {"value": "New2", "text": "New Option 2"},
        {"value": "New3", "text": "New Option 3"}
      ]
    }
    

    JavaScript:

    $.ajax({
      url: "http://jsbin.com/apici3",
      dataType: "json",
      success: function(data) {
        var options, index, select, option;
    
        // Get the raw DOM object for the select box
        select = document.getElementById('theSelect');
    
        // Clear the old options
        select.options.length = 0;
    
        // Load the new options
        options = data.options; // Or whatever source information you're working with
        for (index = 0; index < options.length; ++index) {
          option = options[index];
          select.options.add(new Option(option.text, option.value));
        }
      }
    });
    

    Live example

    Directly from an object:

    If you’re using the object’s property names as option values, and the property values as the option text:

    JSON:

    {
      "new1": "New Option 1",
      "new2": "New Option 2",
      "new3": "New Option 3"
    }
    

    JavaScript:

    $.ajax({
      url: "http://jsbin.com/apici3/2",
      dataType: "json",
      success: function(data) {
        var name, select, option;
    
        // Get the raw DOM object for the select box
        select = document.getElementById('theSelect');
    
        // Clear the old options
        select.options.length = 0;
    
        // Load the new options
        for (name in data) {
          if (data.hasOwnProperty(name)) {
            select.options.add(new Option(data[name], name));
          }
        }
      }
    });
    

    Live Example


    Update: Rather than

    select.options.add(new Option(...));
    

    you can also do:

    select.options[select.options.length] = new Option(...);
    

    Live example

    …which I think actually I would tend to use over the add method on the options array-like-thing (I’m not calling it an array because it has a method, add, that arrays don’t have; and because if you use push, which arrays do have, it doesn’t work).

    I’ve tested both methods on

    • IE6,7,8 (Windows)
    • Chrome (Linux & Windows)
    • Firefox (Linux & Windows)
    • Opera (Linux & Windows)
    • Safari (Windows)

    …and the both work. Perhaps someone with a Mac could try Safari on OS X for me.

    I’d say both ways are very, very well supported.

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

Sidebar

Related Questions

If I have an object implementing the Map interface in Java and I wish
I have this object graph, I want to map: abstract Account (username, password, ...)
I have a class that map objects to objects, but unlike dictionary it maps
Ok, NHibernate question here. I have two objects that I would like to map
I have unsorted map of key value pairs. input = { xa => xavalue,
I have a Map which is to be modified by several threads concurrently. There
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have a map for my game, I have a script that on a
I have a map of a building and I'd like to programmatically highlight specific
I want to have a map that has a homogeneous key type but heterogeneous

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.