I can’t figure out how to store the value of $(this) in a variable for use in another function.
In this example I want a reference to the clicked div element:
function doSomething(thisVar) {
// do stuff with thisVar
}
$("div.someClass").click(function() {
var myThisVariable = $(this);
doSomething(myThisVariable);
});
It doesn’t work (becomes a NULL reference). What is the proper jQuery syntax?
Edit: This error was down to me confusing different instances of this. It serves to show that you need to pay extra attention to what this really means in each context.
This works for me. Your problem is probably elsewhere.
This IS the proper jQuery way to do it. Very often developers save the value of
$(this)to a globalself(or$self) to access it in cases where the context and thus the value ofthischanges. Passing it as an attribute is almost the same thing.