Gerardo
Gerardo

Reputation: 1948

Error with "this" in IE with jQuery plugin

I'm coding a plugin for jQuery which now works fine on every browser but IE. This is part of the code:

(function( $ ){
    $.fn.myPlugin = function(options) {

        var methods = {
            getFirstList: function(el){
                return $("ul:first", el);
            }
        };

        return this.each(function(){
            ...
            var list = methods.getFirstList(this); 
            // "this" here refers to window or document in IE.
            ...
        });
    };
})( jQuery );

When I call the plugin ($("#myObject").myPlugin();), the keyword "this" is not refered to the DOM object, but to the window or document.

How do I fix this?

Upvotes: 3

Views: 89

Answers (1)

Matt Williamson
Matt Williamson

Reputation: 40193

Try replacing this with $(this)

Upvotes: 2

Related Questions