kjtocool Posted June 19, 2008 Share Posted June 19, 2008 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 19, 2008 Share Posted June 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
kjtocool Posted June 19, 2008 Author Share Posted June 19, 2008 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! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 19, 2008 Share Posted June 19, 2008 For the record, eval is something to steer away from if at all possible. Using it opens you up to many potential problems. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.