Jump to content

[SOLVED] Using JS to change selected value of a dropdown


kjtocool

Recommended Posts

How do you do this?  I am basically trying to use javascript to re-set the selected value of a html dropdown box.  How can I do this?

 

var dropdown = eval("movie_rankings.movie_" + old_rank + "_rank");

dropdown.option[0] = ??

 

Something along those lines?

Link to comment
Share on other sites

What is the purpose of eval? A better option, IMHO, would be this:

 

var dropdown = movie_rankings['movie_' + old_rank + '_rank'];

 

Then you can simply set the selected value like this:

 

dropdown.value = 'newvalue';

 

As long as newvalue exists in the values of the options options it should be selected. Note that the value of an option is not necessarily the same as the text of the option (which is what the user sees). To set the option of a select list based upon the text you must iterrate through every option until you find a match.

Link to comment
Share on other sites

eval does exactly what your method did, I used it only because I knew it worked and was unaware of the other option.  Your way is surely more efficient, thanks for the tip!

 

As for the real issue, it worked like a charm, thank you a ton!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.