0

For some reason I am having a hard time using the value of an option in a select dropdown. Here is the code:

<select id="select_category" name="select_category"> <option value=""></option> <option value="home">Home</option> <option value="office">Office</option> <option value="dinning_room">Dinning Room</option> </select> <script> var select_category = document.getElementById("select_category"); select_category.options["home"].selected = true; </script> 

When I run this I get the error "Cannot set propery 'selected' of undefined. If I replace ["home"] with [2] in the last line it works. Why can't I reference the option via the value I assigned to it?

Thanks

EDIT - as suggested, changed the javascript to this below, but now I get the error "token ILLEGAL"

document.getElementById('select_category').value = 'home';​​​​​​​​​​ 
1
  • document.getElementById('select_category').value = 'home';​​​​​​​​​​ Commented Jul 26, 2013 at 17:36

4 Answers 4

3
document.getElementById('select_category').value = 'home';​​​​​​​​​​ 

jsFiddle Demo

Sign up to request clarification or add additional context in comments.

7 Comments

This is actually a much better way to do it than what I suggested.
But how do I then set the selected item to that one?
@LuisP That's exactly what it does. Use the jsFiddle demo I posted to try the other options.
@DevlshOne I cannot get it to work. Essentially this is for a dynamically created select statement for which I want to set the option which I receive via a variable. When I removed my two lines of JS and just added yours I got the error "Unexpected token Illegal"
If it's for a dynamically created select statement, you're going to need to use jQuery so you can delegate events or add .addEventListeners in your JS. Are you trying to stick to pure JS?
|
2
select_category.options["home"].selected = true; 

This line isn't working because options requires an index (ie the number, as you discovered above).

http://www.w3schools.com/jsref/coll_select_options.asp

Give each option a name and use this:

select_category.options.namedItem("nameGoesHere").selected = true; 

Alternatively, you can give each option an id and use:

document.getElementById("home").selected = true; 

(assuming the id of the option is "home" in this case.

5 Comments

How can I then set a certain option as selected using the value (should I put an ID on each option?)
If you put an id on each option that would be fine. Value is just used to display text to the user. Since you can potentially have two options with the same value, it isn't useable as a unique identifier. With id's, if you have id="home", then you know exactly which one options["home"] is referring to since you can't have more than one option with id="home". That's why value isn't used as an identifier.
Ok, tried doing that and now get the error "unexpected identifier". now each option is defined <option id="home_option" value="home">Home</option>.... and the JS is: var select_category = document.getElementById("select_category"); select_category.options["home_option"].selected = true;
You can actually just do document.getElementById("home_option").selected = true;
I lied to you. Sorry. I re-did my answer.
2

Because options is an array. You could write a function like this, though:

function selectOptionByValue (select, value) { for ( var i = 0; i < select.options.length; i++ ) { select.options[i].selected = select.options[i].value === value; } } 

Comments

1

Try this one:

document.getElementById("select_category").options[1].selected = true;

If you want to set it via the value use this:

document.getElementById('select_category').value = 'home';​​​​​​​​​​

4 Comments

Yes, but I need to do it via the values since this is coming from a variable
I tried doing what you said but now I get a 'token illegal' error (see edit above).
Do you want to set the value, before the element gets rendered?
No, not necessary, I just want it to show the right option (which was the one selected in the prior page)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.