Search code examples
javascriptjqueryfunctionchaining

Understanding method chaining in javascript


I want method chaining to work, but it seems i'm not getting some concepts.

This:

$(".list").activeJS()

first has to use jQuery to get a HTMLElement nodelist and then it has to call the activeJS() function passing the nodelist.

var activeJS = function(item){

    alert(item) //nodelist


}

Now i get a TypeError: $(...).activeJS is not a function error in my console.

Thx,


Solution

  • If you want to create a function callable from a jQuery object, you have to add it to the jQuery prototype object:

    jQuery.fn.activeJS = function(item) {
      // ... whatever
    };