tags:

views:

23

answers:

3

Finding out the current value of a select-list with jQuery 1.4 is easy:

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

But whats about jQuery 1.2? I get an "undefinied".

I also tried

$('#selectList:selected').val();
$('#selectList option').val();
$('#selectList option:selected').val();

Nothing works but for this project i have to use jQuery 1.2

+1  A: 

You should be able to do this using :selected and .attr():

$('#selectList :selected').attr('value');

Though, .val() should be working (since jQuery 1.0), is there possibly a plugin breaking it? (Or, is Prototype in the page?)

The above is just approaching it differently, it gets the selected <option> and gets its value="" attribute directly, if that doesn't work, something is very wrong.

Nick Craver
+1 forgot to see he was using val
Sarfraz
A: 

Sorry .val() works in jQuery 1.2, too. my mistake

TorbenL
+2  A: 

I'm not sure why val() isn't working for you, but you can get the element and use the plain DOM property, value:

$('#selectList')[0].value;

// or
$('#selectList').get(0).value;
Andy E
`:selected` = 1.0 :) http://api.jquery.com/selected-selector/ Also, `<select>` doesn't have a `value` property, more sleep Andy!
Nick Craver
@Nick: yeah, I double checked `:selected` after I wrote it and removed that part. 1.3 was when they rewrote the selector engine (and created Sizzle) which is why I thought that. However, `<select>` does have a *value* property - paste this into any current browser's console: `"value" in document.createElement("select")` :-)
Andy E
@Andy - Hmm that *shouldn't* work, it's definitely not standard but maybe IE6 is the only reason I've never seen it used. +1 for an educational experience :) ...I never thought to try it since it's not in the HTML4 spec, kudos sir
Nick Craver
@Nick: it works in IE6 too, and it's defined in [DOM level 1 under HTMLSelectElement](http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-94282980) :-)
Andy E
+1 - Although there is an IE6 issue where if you don't have the `value` attribute of the `<option>` elements set, it will not use the text content of the `<option>` in its place like other browsers do.
patrick dw
@patrick: yes, that is one potential issue I had overlooked and one you've pointed out to me before, I think. I'll try to remember it in future :-)
Andy E