JQuery.prop () compatibility

I am trying to check if a method exists .prop()in the current jQuery (to ensure compatibility) with:

if(typeof $.prop === 'function')

I would expect the condition to be higher truefor jQuery >= 1.6and falsefor jQuery < 1.6, as I can understand from the docs

In any case, testing this parameter with jsfiddle results in:

typeof $.prop === 'function':

  • truewhen jQuery >= 1.6
  • falsewhen jQuery < 1.6 and jQuery > 1.3
  • truewhen jQuery <= 1.3

here is a very simple script that provides the results above (just switch the jQuery version to see what I described).

When I try to use this .prop()with jQuery ie 1.3, I get an error .prop is not a function. The same problem is also tested outside of jsfiddle. Is this behavior normal? How can I check if there is .prop()?

thank

+5
3

alert(typeof $.fn.prop === 'function')

.prop jQuery, $.fn. 1.3.

jQuery ( ).

+7

, prop, jQuery, 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);

: http://jsfiddle.net/JtK2Q

+6

You check for a static method.

You need to check the instance method by writing $.fn.prop( $.fnmatches $.prototype).

+4
source

All Articles