David West
David West

Reputation: 2328

click handler in jquery and changing css background color

I've been experimenting with jQuery. It has a click handler that I'm wondering about.

This works:

$('div').click(function() {
    alert("Zing!");
});

This doesn't:

$('div').click(function() {
    this.css("background-color","blue");
});

can I change the color of the selected div with .click()? Here's a fiddle http://jsfiddle.net/RyPgT/

All the examples in the jQuery API are using .on("click", function(){...}); Are there other, better ways?

Upvotes: 0

Views: 76

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382170

Just change your code to this :

$('div').click(function() {
    $(this).css("background-color","blue");
});

The problem is that this is a standard DOM element, without the css function, that's why you must make it a jQuery element using $(this).

Upvotes: 6

Related Questions