Is there some way to add methods to jQuery’s objects?
For example, I have jQuery object
a = $('div')
I’d like that every object which was assigned like that, would have particular method (doSomething()) so I could invoke it like
a = $('.foo')
a.doSomething()
b = $('.bar')
b.doSomething()
You have to add your function to the
$.fnnamespace. Please note that inside the function,thiswill refer to the jQuery object, not a DOM object.
jsFiddle Demo
Please read about writing jQuery plugins for additional guidelines.