Friend
Friend

Reputation: 1346

using $(this) inside jquery plugin

Here is my plugin code below.

$.fn.myplugin = function(options) {

    var defaults = {
        my_data_title11:"",
        my_div:".getslow",
    }

    var settings = $.extend({}, defaults, options);

    return this.each(function(){
        console.log(options.my_data_title); 
    });
}

I am calling this plugin from an external page like this...

$('#o_vari,#0_var2').myplugin({
    my_data_title11:$(this).attr("id"),my_div:'.getfast'
});

but it is displaying undefined. I am aware my_data_title11:$(this).attr("id") is not recognizing $(this) the calling statement, I even tried putting this exteral variable , but still the same problem.

Upvotes: 0

Views: 68

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

From the way you have written the code, I'm assuming you wants to print o_vari and 0_var2... but that is not the case because in the context in which you have used $(this).att('id'), this does not refer to the o_varx element.

So if you want the desired output try

$('#o_vari,#0_var2').each(function () {
    $(this).myplugin({
        my_data_title: this.id,
        my_div: '.getfast'
    });
})

Demo: Fiddle

Upvotes: 1

Related Questions