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
@Reigel's answer is great! However you could also use the $.fn
syntax and let your function only handle certain elements:
$.fn.someFunction = function(){
this.each(function(){
// only handle "someElement"
if (false == $(this).hasClass("someElement")) {
return; // do nothing
}
$(this).append(" some element has been modified");
return $(this); // support chaining
});
};
// now you can call your function like this
$('.someElement').someFunction();
See working jsfiddle: http://jsfiddle.net/AKnKj/3/