views:

86

answers:

2

I'm writing a page where I need to get the value attribute for the selected option within a select tag.

The way I usually do it like this: onchange="changeCurrentWebsite(this.options[this.selectedIndex].value)

However every time I use ".value" in Javascript my IDE(Intellij) complains that that symbol is deprecated... Does anybody know the correct way to do it?

+3  A: 

Intellij is incorrect. 'Value' is perfectly valid.

Check W3C Documentation:

http://www.w3.org/TR/html401/interact/forms.html#h-17.6

SolutionYogi
I deleted my comment, you are correct.
Dennis Baker
A: 

Instead of

this.options[this.selectedIndex].value

is it not possible to just use the "value" property of the element ? Like this :

document.getElementById('ID_OF_SELECT').value

Or, in your case :

changeCurrentWebsite(this.value)

Does you IDE say it's deprecated too ?

Pascal MARTIN
changeCurrentWebsite(this.value) works, and No, my IDE does not complain about that! I like it, much better looking than this.options[this.selectedIndex].value. Thx!
hdx
you're welcome :-)(better looking... and shorter to type and read -- which is a good thing ^^ )
Pascal MARTIN
Actually, 'select' tag doesn't have value property. You have to find the 'selected' option and use its value property.
SolutionYogi
ho, seems you're right, if I look ath the specifications :-((strange thing is, when I trying using it with firebug, it's there... but as it's not in the specs, shouldn't be used :-( )
Pascal MARTIN
@Pascal: `this.value` works in all modern browsers and IE6. You're fine reading from and writing to it without going through the `options`. Infact, `mySelect.value = 'foo'` is orders of magnitude faster than manually iterating options looking for the matching `option.value` and setting the `selectedIndex`.
Crescent Fresh
@crescentfresh thanks ; that's good to know, especially about IE6 (I don't have IE at home, so couldn't test this morning when I added my last comment -- and as IE's not always... well, you get what I mean :-D )
Pascal MARTIN