I have a dynamically populating table and would like to add a mouseover feature
(mouseover="style.backgroundColor='#E1EBF4'")
to my table row while the data row is being added.
function addRecentData(data) {
$('#newTable tr:last').after('<tr><td class="name"></td><td class="id"></td></tr>');
var $tr = $('#newTable tr:last');
$tr.find('.name').html(data.Name);
$tr.find('.id').html(data.Id);
}
EDIT: Note that if you’re not concerned about IE6 support, you could use the CSS
:hoverpseudo selector to change the background. This should probably be the first consideration.Given your current code, you could just use your
$trreference to the table:Another approach would be to use
inserAfter()instead ofafter(), and assign the variable immediately.Or if each
<tr>should get the mouseover, you could use.delegate()on the#newTableto take care of the mouseover.Now
<tr>elements will automatically get the functionality you want when they are added to the table.