I have something like this:
<tr id='<%=currentRow %>' onclick="SetBackgroundColor(this)" style="background-color:Yellow">
When i click on a row i want to change its background color and i did like this:
function SetBackgroundColor(rowId)
{
$(rowId).css("background-color", "#000000");
}
but i don't know why it doesn't work. Any suggestions please?
The HTML <tr> bgcolor Attribute is used to specify the background color of a table row. It is not supported by HTML 5. Attribute Values: color_name: It sets the background color by using the color name.
Setting the Background Color of Table Rows. The bgcolor attribute is now deprecated, but it was once the correct way to control the background color of table rows. Color names (such as “blue”), hex numbers, and rgb color codes could all be used with the bgcolor attribute.
What to Know. Easiest: add the style property background-color to the table, row, or cell tag. Next easiest: use the attribute bgcolor.
IE has a problem with background colors for the TR element. A more safe way is to set background to the TD's and TH's inside the TR:
<table id="tabletest">
<tr>
<td>testcell</td>
</tr>
</table>
<script>
$('#tabletest tr').bind('click', function(e) {
$(e.currentTarget).children('td, th').css('background-color','#000');
})
</script>
Added: you can assign a single event handler for the entire table to increase performance:
$('#tabletest').bind('click', function(e) {
$(e.target).closest('tr').children('td,th').css('background-color','#000');
});
In jQuery you do not have to use the onclick attribute to assign an event handler. Lets say you add a class called mytr to each tr that you want to affect. Then you can do something like this:
$(document).ready(function(){
$(".mytr").click(function(){
$(this).css("background-color", "#000000");
});
});
And that will apply the event handler to all rows with the class mytr.
This will reset each row upon clicking a new one...
$(document).ready(function(){
$('tr').click(function(){
$('tr td').css({ 'background-color' : 'green'});
$('td', this).css({ 'background-color' : 'red' });
});
});
demo: http://jsbin.com/aciqi/
$('#RowID').children('td, th').css('background-color','yellow');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With