Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery add onmouseover attribute

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'">
like image 690
user1022585 Avatar asked Jul 18 '12 00:07

user1022585


2 Answers

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');
});
like image 119
undefined Avatar answered Nov 25 '22 12:11

undefined


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');
});
like image 45
silentw Avatar answered Nov 25 '22 11:11

silentw