Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery 1.6, changed its .val() behavior?

In jQuery 1.6, it seems to me that the .val() function has been changed since jQuery 1.5.2.

When I call it on a select's option to get a value, now I get the text. For example:

<option value="1">john</option>

When I call:

$('select').val();

with jQuery 1.5.2 I get 1 while with jQuery 1.6 I get john

I know they changed behavior of .attr() and introduced .prop(), but at this link I'm not able to find anything about .val().

Here is an example that shows this difference and what I've tried so far to get the option's value. Switch from jQuery 1.6 to jQuery 1.5.2 to see.

How do I get an option's value with jQuery 1.6?

like image 845
Dalen Avatar asked May 01 '26 19:05

Dalen


1 Answers

Your mistake in your jsfiddle example is to use val to create the value, for this you should rather use attr('value', id). Then val() still works as expected, the same as in previous versions, and you can get the option value with it.

http://jsfiddle.net/KFDWm/5/

like image 145
Herman Schaaf Avatar answered May 03 '26 10:05

Herman Schaaf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!