Reputation: 2328
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
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