eKini: Web Developer Blog

PHP, MySQL, Javascript, MVC, Zend Framework, AJAX, jQuery

Hightlight a table row on click using jQuery

February19

The scenario: Every time you click on a row, the row highlights. When you click on the highlighted row - remove the highlight.
The jQuery / Javascript code:

  1.  
  2. //highligths the clicked row
  3. function highlightrow(obj) {
  4.     if ($(obj).attr("style")==‘background-color: rgb(255, 255, 187);’ || $(obj).attr("style")==‘BACKGROUND-COLOR: #ffffbb’) {
  5.         $(obj).removeAttr("style");
  6.     } else {
  7.         $(obj).attr("style","background-color: #FFFFBB;");
  8.     }
  9. }
  10.  

And for the HTML Code:

  1.  
  2. ..rest of the HTML tags here…
  3. < tr onclick="highlightrow(this);" >
  4. ..rest of the HTML tags here…
  5.  
posted under General
2 Comments to

“Hightlight a table row on click using jQuery”

  1. On February 19th, 2008 at 3:31 pm Wenbert Says:

    The reason for having this:
    background-color: rgb(255, 255, 187);
    and
    BACKGROUND-COLOR: #ffffbb
    is because Firefox returns: background-color: rgb(255, 255, 187); while IE returns something like this: BACKGROUND-COLOR: #ffffbb

  2. On August 22nd, 2008 at 1:29 pm Chris Says:

    Hey wenbert, try not using .attr(’style’) but rather use the built-in jQuery css functions!
    Your code will mess up style, if you have other style definitions in the tag…

    JS:

    $(document).ready(function(){
      $("tr").click(function() {
        $(this).toggleClass("active");
      }
    });
    

    CSS:

    .active { /*highlight class, add a background color, change the border or font color, whatever */
      border: 1px dashed red;
      background-color: green;
    }
    

    HTML:
    ………

Email will not be published

Website example

Your Comment: