Nuvolari
Nuvolari

Reputation: 1155

JQuery Change Table Cell And Row Background Color On Hover

Basically when I hover over a row in my table i want the background color of the row to change to say 'black' and the specific cell or td I am hovering over to change to 'red'.

Ie so two events occur when hovering. I know how to do it for one or the other but not both.

Is this possible using jquery?

Thx to everyone for their contribution, I've repped you all.

Upvotes: 3

Views: 15187

Answers (7)

Mrugesh Vyas
Mrugesh Vyas

Reputation: 11

$(function() {
	$(".tablecell").on('mouseover', function(event) {  
		$(".tablerow td").removeClass('hoveColBgColor hoveRowBgColor');

		$(this).parent('tr.tablerow').children('td:gt(0)').addClass('hoveRowBgColor');
		$('.tablerow > td:nth-child('+($(this).index()+1)+')').addClass('hoveRowBgColor');
		
	});
	$(".tablerow").on('mouseout', function(event) {  
		$(".tablerow").children('td:gt(0)').removeClass('hoveRowBgColor');
		$(".tablerow td").removeClass('hoveColBgColor hoveRowBgColor');
	});
});
.hoveRowBgColor{
	background-color:#ccc;
}
.hoveColBgColor{
	background-color:#666;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table id="table1" width="100%" cellspacing="1" cellpadding="0" bordercolor="" border="0">
  <tbody>
    <tr class="tablerow">
      <td class="whiteZone">&nbsp;</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
  </tbody>
</table>

Upvotes: 1

user2587132
user2587132

Reputation:

Simple CSS is enough:

tr:hover{
 background-color:red
}

td:hover{
background-color:blue
}

http://jsfiddle.net/nRuXn/1/

Upvotes: 3

colestrode
colestrode

Reputation: 10658

Add a hover listener to all rows and td's that adds and removes a class, then use CSS to style that class differently for a row and cell.

Working Demo

jQuery

$('tr, td').hover(function() {
    $(this).addClass('highlight');
}, function() {
    $(this).removeClass('highlight');
});

CSS

tr.highlight {
    background-color: red;
}

td.highlight {
    background-color: black;
}

Upvotes: 2

user2625787
user2625787

Reputation:

$('td').hover( function() {
    $(this).parent().children().css("background-color", "black");
    $(this).css("background-color", "red")
});

$('tr').mouseleave( function() {
    $(this).children('td').css("background-color", "white");// or whatever
});

Upvotes: 3

Armen
Armen

Reputation: 1093

Add some class to that td that you want to be red (lets call that class 'rdClass') and the row 'blkClass':

<table>
<tr class='rdClass'>
 <td>
        1
 </td>
 <td class='blkClass'>
        2
 </td>
 <td>
        3
 </td>
</tr>
</table>

$(document).ready(function () 
{
    $(".rdClass").mouseover(function ()
    {
        $(this).css("background-color", "red");
    });

    $(".blkClass").mouseover(function ()
    {
        $(this).css("background-color", "black");
    });
});

Upvotes: 2

Alireza
Alireza

Reputation: 5673

$("td").hover(function(){
  $(this).css("background-color", "red");
  $(this).parrent('tr').css("background-color", "black");
});

Upvotes: 1

user2483173
user2483173

Reputation: 13

If both the row and cell are in the same container, you could attach a mouseover event to that container and modify the children in the handler.

Upvotes: 1

Related Questions