tags:

views:

4811

answers:

5

Hi,

In jQuery, what is the equivalent to document.getElementById("selectlist").value ?

I am trying to get the value of a select list item.

Thanks.

+14  A: 
$('#selectlist').val();
ChaosPandion
+3  A: 

Chaos is spot on, though for these sorts of questions you should check out the Jquery Documentation online - it really is quite comprehensive. The feature you are after is called 'jquery selectors'

Generally you do $('#ID').val() - the .afterwards can do a number of things on the element that is returned from the selector. You can also select all of the elements on a certain class and do something to each of them. Check out the documentation for some good examples.

RodH257
A: 

In some cases of which I can't remember why but $('#selectlist').val() won't always return the correct item value, so I use $('#selectlist option:selected').val() instead.

Brett Ryan
Maybe you could post some links back that confirm this. I would be surprised however. This would be a fundamental bug in jQuery. Is it possible you are using a buggy version of the library?
James Wiseman
Could have been James, I think it could have been in 1.3.0, can't seem to repro in 1.3.2, but it could have also been in IE6 or 7 which I don't have installed anymore, I'll see if I can figure out the original cause and report it here.
Brett Ryan
+1  A: 

"Equivalent" is the word here

While

$('#selectlist').val();

is equivalent to

document.getElementById("selectlist").value

its worth noting that

$('#selectlist')

although 'equivalent' is not the same as

document.getElementById("selectlist")

As the former returns a jQuery object, not a DOM object.

James Wiseman
A: 

Check out this: jsFiddle ;)

Nyuszika7H