Would make strUser be 2. If what you actually want is test2then do this:. The following script is getting the value of the selected option and putting it in text box 1. The following script is getting a value from a text box 2 and alerting with its value. Running the above in Firefox et al will give you an 'is not a function' error because IE allows you to get away with using instead of :.
Beginners are likely to want to access values from a select with the NAME attribute rather than ID attribute. We know all form elements need names, even before they get ids. The previous answers still leave room for improvement because of the possibilities, the intuitiveness of the code, and the use of id versus name.
One can get a read-out of three data of a selected option -- its index number, its value and its text. This simple, cross-browser code does all three:.
For functional form purposes, name is still valid, also in HTML5, and should still be used. Lastly, mind the use square versus round brackets in certain places. As was explained before, only older versions of IE will accept round ones in all places. Inin Firefoxthe following also works.
I simply like to forex global solutions cyprus my code as much as possible. If anyone knows how to do this in one line without having to get the element twice, I would love for you to comment and show me how.
I haven't been able to figure that out yet Thank mutual binary options for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site the association bonus does not count.
Would you like to answer one of these unanswered questions instead?
Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site How to buy gold etf in singapore Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.
I tried the methods below but they all return the selected index instead of the value: Danferd Lan 6, 16 40 If you have a select element that looks like this: If what you actually want is test2then do this: Paolo Bergantino k 66 TheRedPea—perhaps because when this answer was written there was a chance however remote that an ancient version of Netscape Navigator needed to be accommodated, so an equally ancient method for accessing the value of a single select was used.
But I'm just guessing about that.
How does it work for multiple select? I used like this: This did worked for me in different way.
Is it the text you're after? Greg k 39 Thanks for example given! Yep - make the value of the option the same as what it is. Simpler - the guy above needs to write more code to make up for his initial vagueness.
If you ever run across code written purely for IE you might see this: Clara Onager 2, 21 DesertIvy 4, 6 20 Mohammad Faisal 1, 1 11 So, I'm adding the getElementByName solution just for new developers to see too.
Ben Greenaway 1 2. Marvil Joy 4 This simple, cross-browser code does all three: Frank Conijn 1, 11 Hector Llorens 79 5. Genko 2 7.
HTML DOM Select value Property
MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.
Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.