tags:

views:

377

answers:

3

I am try to write a jQuery statement to change the table row that is currently clicked to a different foreground color while making all the sibling table rows the same color. Basically making it look like the clicked row is active. Here's what I have so far, this here works the first time, but on the second time on it never sets all the siblings to the non selected color.

<script type="text/javascript">
    function changeRows(currentRow) {
        $(currentRow).css('color', 'green'); // change clicked row to active color
        $(currentRow).parent().siblings().css('color', 'red'); // change all the rest to non active color
    }
</script>  


<table>
  <tr>
    <td onclick="changeRows(this)">123
    </td>
  </tr>
  <tr>
    <td onclick="changeRows(this)">1234
    </td>
  </tr>
  <tr>
    <td onclick="changeRows(this)">12345
    </td>
  </tr>
  <tr>
    <td onclick="changeRows(this)">123456
    </td>
  </tr>
</table>
+2  A: 

It's best to handle this type of stuff through classes rather than popping in ad-hoc css rules.

$("td").click(function(){
  $(this).addClass("on").parent().siblings("tr").find("td").removeClass("on");
});

Online demo: http://jsbin.com/ajalu/2/edit

Or you can base it on the TR itself:

$("tr").click(function(){
  $(this).addClass("on").siblings("tr").removeClass("on");
});

Online demo: http://jsbin.com/ajalu/3/edit

Jonathan Sampson
There are two things I don't like about this. First, it applies the classes to the table elements, not the rows. It seems to me that highlighting a row ought to be a row operation, not an element operation. Second, it really depends on the table structure being uniform and pretty vanilla to work. For example, it won't affect rows in multiple tbody elements. It also only highlights the current element in the selected row. If the row contains more elements, those elements don't get the class applied.
tvanfosson
@tvanfosson: I've updated the solution to address your issues. Thanks.
Jonathan Sampson
A: 

instead of using onclick in your html markup use

If you have an id on each td you could do

$('#td1').click(function() {
   $('td').removeClass('.colored'); 
   $(this).addClass('.colored'); // change clicked row to active color    
});
Catfish
This is out of scope for my question, I will optimize it after I have the functionality working.
gmcalab
I edited my answer with a better solution than what i had before.
Catfish
still wrong and syntax is also incorrect removing and adding classes. you don't include the '.'
gmcalab
yea your right don't need the .I'm going to off myself since you caught me in error twice now
Catfish
+1  A: 

I would prefer doing this by adding/removing CSS classes rather than setting CSS directly. I think your issue though is that you're setting the css on the column element rather than on the rows themselves. You might want to try something like:

<script type="text/javascript"> 
    function changeRows(currentElem) {
        var $currentElem = $(currentElem);
        var $currentRow = $currentElem.closest('tr');
        var $table = $currentElem.closest('table');
        $table.find('tr').removeClass('highlight');
        $currentRow.addClass('highlight');
    } 
</script>
tvanfosson