onmouseover and inline function using the 'this' object

Matt picture Matt · Jul 16, 2011 · Viewed 14.7k times · Source

I have the following bit of code:

<div id="some_div" style="border: solid 1px red; width:50px; height: 50px;" 
onmouseover="(function(){$(this).css('background','green');})();"></div>

The function gets run just fine but it doesn't seem to be able to find the 'this'. How can I make it so that it will know to self-reference?

(Yes, jquery is referenced in my code)

Answer

Paul picture Paul · Jul 16, 2011

I don't know if you want to be using an anonymous function there. I think

<div id="some_div" style="border: solid 1px red; width:50px; height: 50px;" 
onmouseover="$(this).css('background','green');"></div>

will work.