5

I've been trying to figure out how to set a value to a an option in a select type. But no matter how much I try to understand I just can't (feels ashamed)..

So I was hoping that you guys could help me since you've helped me so many times before.. :)

Let's say we have:

<select name="box" id="test">    
<option value='tval'>Content</option>

shouldn't this next code change the text from 'Content' to 'box'?

function changeContent(form){
form.document.getElementById('test').options['tval'].value = 'box';
}

or am I completely wrong here? I've looked up so many different articles but no one could help me understand how to do this..

Thanks guys!

5 Answers 5

11

If You need to change the text rather than the value;

function changeContent(){
     document.getElementById('test').options[0].text = 'box';
}

To set both the value and text;

function changeContent(){
     var opt= document.getElementById('test').options[0];
     opt.value =  'box';
     opt.text = 'box';
}
Sign up to request clarification or add additional context in comments.

6 Comments

tried this, does not work.. If this is accurate then i'll have to check the other bits of code to see if i made an error.. which i dont think.. this is really tricky when you're new to it.. =) thanks for answer=)
@Mr.Havoc it is probably because you are doing form.document, try just document.getEle...
Thanks.. the form. was the problem... Why is that?? The JS and HTML are in different files... confused thanks everyone who answered!
@Mr.Havoc I assume form was a DOM element and as such that object wont have access to the document variable (form.document was probably throwing a Javascript error in your console), that is only present in window.document or simply put just document.
@Mr.Havoc click the spanner next to the address bar, go to Tools > Javascript Console
|
1

No, value is the actual value of the option element, what will be sent when the user submits the form. What you are trying to access is the HTML of the option element, you would have to access it with something like:

form.document.getElementById('test').options['tval'].innerHTML='box'

1 Comment

like i commented before, does not work.. if this is accurate I'll have to check the rest of my code for errors.. thanks alot for answer.. :)
1

That will change the value from 'tval' to 'box'

I think what you are looking for is the inner text of the option.

W3CSchools.com has an example of what you are looking for in javascript. You did want javascript, correct?

http://w3schools.com/js/tryit.asp?filename=try_dom_option_settext

Comments

1

Drop the form. part and use:

document.getElementById('test').options[0].innerHTML = 'box'; 

See http://jsfiddle.net/hMqFE/

Comments

1

You should use index of the option here. Here is the working example http://jsfiddle.net/LaMJ9/

Code

document.getElementById('test').options[0].value = 'box'; 

Edit

addded alerts for previous and new values at jsfiddle http://jsfiddle.net/LaMJ9/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.