Background
As of jQuery 1.9 the .attr(..)
method no longer returns property values, instead we now have to use .prop(..)
. Unfortunately this also applies to attributes specified via an attributes selector i.e. $("input[value=]")
See http://jquery.com/upgrade-guide/1.9/#attr-versus-prop-
and a good SO discussion on the differences between .attr
and .prop
:
My Situation
I'm currently using selectors like $("input[value=]")
and $("select[value=]")
to select input elements that have no value set. However, this no longer works with jQuery 1.9, instead I'm now doing something like this:
var hasValue = function () { return !!($(this).val().length); };
var hasNoValue = function () { return !($(this).val().length); };
$("input").filter(hasValue);
$("select").filter(hasValue);
My actual selectors are a little larger, checking multiple elements with or without values so now I'm having to split my 1 selector string into multiple selectors with .filter(..) method calls in between.
Question
Is there an equivalent to $("[value=]")
, $("[value!=]")
, $("[value='abc']")
which uses the property instead of the attribute? And if not, is there a cleaner way than using the .filter(hasValue)
and .filter(hasNoValue)
methods?
Thanks
Using
.filter
seems to be the only way, but it's not too bad and you can actually make it a little more accurate by using.val
:If this really is that reprehensible to you, you could create a custom selector.
http://jsfiddle.net/ExplosionPIlls/zaZPp/
EDIT: You may also be able to get away with using
this.value
instead of$(elem).val()
.According to the upgrade guide:
So this answers your first question - there is not a direct equivalent, the point is to use the attribute instead of the property.
Your code seems fine, but if you want to be more standardized and re-usable you could create an additional filter. Like so:
This will allow you to select stuff like that:
And other various combinations you need.
I came across this and just wanted to post another solution I found that works in some situations where some of the other suggested solutions are not appropriate.
Simply add this event handler to your code