I know that you can add new jQuery functions by $.fn.someFunction = function()
However, I want to add functions to specific elements only. I tried this syntax and it doesn't work $('.someElements').fn.someFunction = function()
I want to do this so that I can call the function like this somewhere in the code $('someElements').someFunction();
The most obvious solution is to assign a function as the object's property:
Then call it on the object this way:
Note: your function is the return value of the outer one, see: http://api.jquery.com/prop/#prop-propertyName-function
yo can do the above with this:
Test: http://jsfiddle.net/DarkThrone/nUzJN/
I did this and its working fine..
and call function from your dynamic function which is creating a dynamic control in html
Use
.bind()
and.trigger()
I actually had this use case as well, but with a cached object. So I already had a jQuery object, a toggle-able menu, and I wanted to attach two functions to that object, "open" and "close". The functions needed to preserve the scope of the element itself and that was it, so I wanted
this
to be the menu object. Anyway, you can just add functions and variables all willy nilly, just like any other javascript object. Sometimes I forget that.Yo, needed to do the same thing, came up with this. its nice cause you destroy the element and function goes poof! I think...