I want to reference an element once and then reuse it threw out my code but I’m not sure the correct way. Below is what I tried but it only works in some places. (EDIT: works, had an error farther up in my code, but I’m still curious if this is the best practice or not.)
$(document).ready(function() {
var image = $("#content img");
image.wrap("<span />");
image.hover(function(){}, function(){});
});
Your code looks fine to me… Making a local variable inside the callback, like you did, is always going to be better than using a global object. However, the beauty of jQuery is that you can shorten your code to not even use a variable at all:
This is called chaining and allows you to “chain” methods of the jQuery object together in one expression. But if you really need the variable for whatever reason, I see nothing wrong with it.