this is a special variable in Javascript, it contains the context of the function call. When you call an object property in the form object.function() , this gets the value of the object. If you do not call the function this way, this defaults to the window object. Since you are not calling a property, the latter happens.
An easy way to solve your problem is to pass the element as an argument.
function resetColor(element) { element.css({ color: "red" }); } $(".class").click(function() { resetColor($(this)); };
Another way is to bind the event directly to the function:
$(".class").click(resetColor);
Or you can do what jQuery does inside, which should use .apply or .call , which allows you to explicitly specify the context:
$(".class").click(function() { resetColor.call(this); });
source share