jQuery .prop() compatibility
Solution 1
alert(typeof $.fn.prop === 'function')
You want to check for the .prop
method on the jQuery prototype which lives on $.fn
. This is false in 1.3.
Also I would avoid feature detection for jQuery versions and instead support a particular version (and up).
Solution 2
What I did, is build a compatible function for prop, for jQuery versions, which doesn't use prop:
(function($){
if (typeof $.fn.prop !== 'function')
$.fn.prop = function(name, value){
if (typeof value === 'undefined') {
return this.attr(name);
} else {
return this.attr(name, value);
}
};
})(jQuery);
You can test this code: http://jsfiddle.net/JtK2Q
Solution 3
You're checking for the existence of a static method.
You need to check for the instance method by writing $.fn.prop
($.fn
is the same as $.prototype
).
Dalen
Updated on June 17, 2022Comments
-
Dalen almost 2 years
I'm trying to test if the
.prop()
method exists on the current jQuery included (for compatibility reason) via:if(typeof $.prop === 'function')
I would expect that the condition above is
true
forjQuery >= 1.6
andfalse
forjQuery < 1.6
as I can understand from the docsAnyway, testing this on jsfiddle, leads to:
typeof $.prop === 'function'
is:-
true
whenjQuery >= 1.6
-
false
whenjQuery < 1.6 and jQuery > 1.3
-
true
whenjQuery <= 1.3
here is the very very simple script which provide the results above (just switch jQuery version to see what I've described).
When I try to use that
.prop()
with jQuery i.e. 1.3 I get the.prop is not a function
error. The same problem occours also testing outside jsfiddle. Is it normal such a behavior? How could I truly test if.prop()
is available?Thanks
-
-
Raynos almost 13 yearsIt's the same as
$.fn.init.prototype