Jquery add onmouseover attribute

in jquery how to add an 'onmouseover' event to an element.

eg,

<tr id=row bgcolor=white> 

becomes

  <tr id=row bgcolor=white onMouseOver="this.bgColor='red'"> 
+4
source share
3 answers

You can use the attr method:

 $('#row').attr("onMouseOver", "this.bgColor='red'") 

But since you are using jQuery, I would recommend using the on method:

 $('#row').on('mouseover', function() { $(this).css('background-color', 'red'); }); 
+11
source

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 on the page:

 var $row = $('#row'); $row.on('mouseover',function(){ $row.css('background-color','red'); }); 
+1
source

Do not add an attribute. Use an event.

 $('#row').mouseover(function() { $(this).css('background','red'); }); 
0
source

All Articles