Reputation: 127
how to change background-color td with jquery ?
I need to change the background-color column one in row two and three and four
my table :
<table class="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td >td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
</tbody>
</table>
demo : jsfiddle
How to do it with jQuery?
Upvotes: 3
Views: 42350
Reputation: 127
I found:
$('table.myTable tr:gt(1):lt(3)').find('td:first').css('background-color', 'red');
demo : jsfiddle
Upvotes: 2
Reputation: 292
I don't suggest using jQuery for this kind of styling because it can be a performance issue when ran and if the user has JavaScript disabled, it won't render properly.
What I suggest is using actual CSS to accomplish this task. If it's absolutely necessary to use JS for it, then adding and removing a class from an element would probably be better.
Find an example of what I'm talking about below. http://jsfiddle.net/F7hnL/
CSS
.myTable tr.highlight td:first-child {
background-color : red;
}
HTML
<table class="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
</tbody>
</table>
Upvotes: 0
Reputation: 33870
If would use this code :
$('.myTable tbody tr').filter(function(i){
return {1 : true, 2 : true, 3 : true}[i];
}).children(':first-child').css('background-color', 'red');
But of course, it would be easier to use class.
Upvotes: 0
Reputation: 10994
$('.myTable tbody tr:lt(4):gt(0)')
.children('td:first-child').css('background-color', 'lightblue');
This gets the tr
with index between 0
and 4
=> 1,2,3
tr:lt(4):gt(0)
Upvotes: 1
Reputation: 1957
You can use the nth selector in jQuery:
http://api.jquery.com/nth-child-selector/
$( "table tr:nth-child(2) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(3) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(4) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(5) td:nth-child(1)" ).css("background-color", "blue");
Upvotes: 7
Reputation: 674
I would add a class to the tds you are concerned with. A good name for the class would be something that describes why you are highlighting. Then you could just add a statement in css that will handle that. Or if you want to change the color via jquery you could do the following.
//Assuming highlighter is your added class name and red is the color you want to change to.
$('td.highlighter').css('background-color', 'red');
Your html for tr would look like this
<tr>
<td class='highlighter'>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
Upvotes: 0