This function is supposed to change the background color of the object being clicked
function colorMe(){
$(this).css('background-color', 'red');
}
I call it like this
$('.colorme').click(colorMe);
and it changes the background of this div
<div class="colorme">Color Me</div>
The problem is that I want to do something else before running colorMe. So I can’t use just $('.colorme').click(colorMe);. What I’m trying to do is something like this
$('.colorme').click(function(){
alert('something happens first, then colorMe is called');
colorMe(); //I call colorMe here..
$(this).colorMe(); //I also tried this, but it's not working
});
but it’s not affecting the div. I think it lost track of the div to affect. Do I need to pass it and how?
To call a function on a jQuery object like you did here
you would have to build a plugin (I edited it to add a class)
Then you would be able to do