user1022585
user1022585

Reputation: 13651

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'">

Upvotes: 5

Views: 15342

Answers (3)

Ram
Ram

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

silentw
silentw

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

Moin Zaman
Moin Zaman

Reputation: 25455

Don't add the attribute. Use the event.

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

Upvotes: 0

Related Questions