jQuery .val() vs .attr("value")
Solution 1:
There is a big difference between an objects properties and an objects attributes
See this questions (and its answers) for some of the differences: .prop() vs .attr()
The gist is that .attr(...)
is only getting the objects value at the start (when the html is created). val()
is getting the object's property value which can change many times.
Solution 2:
Since jQuery 1.6, attr()
will return the original value of an attribute (the one in the markup itself). You need to use prop() to get the current value:
var currentValue = $obj.prop("value");
However, using val() is not always the same. For instance, the value of <select>
elements is actually the value of their selected option. val()
takes that into account, but prop()
does not. For this reason, val()
is preferred.