Within SomeFunction
, this
is not a DOM element, it's a jQuery instance. (Plugins are very different from event handlers.) That instance may have zero, one, or multiple matching DOM elements inside it.
So this.css("background", "red");
would work. E.g.:
(function($){
$.fn.extend({
SomeFunction: function() {
this.css("background", "red");
}
});
})(jQuery);
If your goal is to use the set from outside SomeFunction
(maybe SomeFunction
does filtering of some kind?), usually you'd do that by doing your filtering and returning the jQuery object with the (filtered) results. (If not filtering, the convention is to return this
[the full set].)
So for instance, suppose SomeFunction
is only supposed to return elements containing the text "kittens"
:
(function($){
$.fn.extend({
SomeFunction: function() {
return this.filter(function() {
return $(this).text().indexOf("kittens") !== -1;
});
}
});
})(jQuery);
Then:
$(document).ready(function(){
$('.someclassname').SomeFunction().css("background", "red");
});
would only affect elements with someclassname
that contained the text "kittens"
: Live Copy | Live Source