Reputation: 13651
in jquery, how do I add an 'onmouseover' event to an element.
eg
<tr id=row bgcolor=white>
becomes
<tr id=row bgcolor=white onMouseOver="this.bgColor='red'">
Upvotes: 5
Views: 15342
Reputation: 144689
You could use the attr
method:
$('#row').attr("onMouseOver", "this.bgColor='red'")
But since you are using jQuery I'd recommend using the on
method:
$('#row').on('mouseover', function() {
$(this).css('background-color', 'red');
});
Upvotes: 12
Reputation: 4885
try this if the element is static:
var $row = $('#row');
$row.mouseover(function(){
$row.css('background-color','red');
});
use this if the element is dynamically placed in the page:
var $row = $('#row');
$row.on('mouseover',function(){
$row.css('background-color','red');
});
Upvotes: 1
Reputation: 25455
Don't add the attribute. Use the event.
$('#row').mouseover(function() {
$(this).css('background','red');
});
Upvotes: 0