Bradley Weston
Bradley Weston

Reputation: 425

Creating a small jQuery plugin

I created this plugin to make work around an application easier.

Here is the link : http://jsfiddle.net/X5Squ/

My problem it that it always uses just 1 of the elements, please don't edit the data and data5 functions as these work perfectly for other parts but I need my function called jtoggle to work.

Any help much appreciated! Thanks.

Upvotes: 0

Views: 122

Answers (2)

jabclab
jabclab

Reputation: 15042

In your plugin you should act on each matched element and then return all the matched elements in order to maintain chainability:

$.fn.jtoggle = function (addUnderline) {
    return this.each(function () {
        // Do what you need on this matched element

    });
};

Maintaining chainability means we can do stuff like:

$(".jtoggle").jtoggle(true).addClass("xyz");

Upvotes: 0

Flater
Flater

Reputation: 13783

$(document).ready(function (){$('.jtoggle').jtoggle(true);});

Have you tried using .each on this? I think the issue is that it isn't passing an array of DOM elements. I lack much experience in creating plugins, but it seems this can be easily averted by doing the following:

$(document).ready(function (){
    $('.jtoggle').each(function(){
        $(this).jtoggle(true);
    });
});

(Which would also mean that you can safely remove the .each you have in jtoggle itself)

Upvotes: 2

Related Questions