I'm using jQuery's .val()
function to read the value of a <select>
tag.
It seems that, at least in Firefox, .val()
returns the value of the option the user is currently hovering over. You can see this behaviour at this jsfiddle.
Is there any way using jQuery or pure javascript to get the value that is shown in the select box, i.e. the last value that actually fired a change event?
Original Idea
function foo() {
var value = $('#select').val();
// do something that depends on value
}
The problem with this is that I only want foo()
to use the value that is currently selected. By selected I mean the option that was clicked. In the fiddle, you can see that this value changes as you hover over options.
Alternative
var value;
$('#select').change(function() {
value = $('#select').val();
}
function foo() {
// do something with value
}
This is OK, but the information appears to exist in the DOM, since the last clicked value is displayed in the select box.
So, my question is, is it possible to get the last clicked option from the DOM?