views:

57

answers:

2

Hi Everyone,

Could someone help me please with my problem below?

I basically have this markup coming from my JSP. I add class in each row and I want have a blinking effect on each row.

<table>
 <tbody>
  <tr class="blinkYellow">
   <td>Col 1</td>
   <td>Col 2</td>
   <td>Col 3</td>
  </tr>
  <tr>
   <td>Col 1</td>
   <td>Col 2</td>
   <td>Col 3</td>
  </tr>
  <tr class="blinkYellow">
   <td>Col 1</td>
   <td>Col 2</td>
   <td>Col 3</td>
  </tr>
 </tbody>
</table>

I setup a Jquery function and a timer like below. But I am currently unsure why the background-color of the table did not change.

$(document).ready(function(){
 setInterval(findYellow,1000);    
 function findYellow(){
  $("tr.blinkYellow").each(function(){
   if($(this).attr("background-color") == "yellow"){
    $(this).attr("background-color", "white")
   }else{
    $(this).attr("background-color", "yellow")
   }
  })
 }
});

I check out the Firebug HTML Tab and I notice that the background-color is really being changed on the selected element row.

But I am losing my hair on why the background color of the row is not toggling its color from yellow and white. Please help

A: 

The attr function will add/change an attribute. Use the css function instead of the attr function.

$(document).ready(function(){
 setInterval(findYellow,1000);    
 function findYellow(){
  $("tr.blinkYellow").each(function(){
   var $this = $(this);
   if($this.css("background-color") == "yellow"){
    $this.css("background-color", "white")
   }else{
    $this.css("background-color", "yellow")
   }
  })
 }
});

Another option would be to set a css style for the blinkYellow class that includes a background-color of yellow, and then toggle it:

var $blinkYellow = $("tr.blinkYellow");
$(document).ready(function(){
  setInterval(findYellow, 1000);    
  function findYellow() {
    $blinkYellow.each(function() {
      // note: jQuery is not really needed here - instead, you could use the following line
      // this.className = this.className == 'blinkYellow' ? '' : 'blinkYellow';
      $(this).toggleClass('blinkYellow');
    });
  }
});
Nate Pinchot
isn't is backgroundColor rather than background-color??
webfac
@webfac http://api.jquery.com/css/ Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css('background-color') and .css('backgroundColor').
Nate Pinchot
@Nate - Thanks for the link, I learned something new, wow. ;)
webfac
@webfac As did I. I used to just use the css stylesheet names for that function, I did not know the DOM ones worked as well :)
Nate Pinchot
@Nate.. Thanks.. I learned the differences between attr and css because of your post... I tried to mark this as an answer also but I just found out that in SO, only one is allowed. I'll keep your thoughts in my future scripts though.. Thank you
Mark Estrada
@Mark No worries. I'm in it to help others - not worried about getting a silly green check mark :) Glad I could help.
Nate Pinchot
+2  A: 

Wouldn't it be better to use css classes to add the color. If you do so, you can use jquery as follows:

$(this).toggleClass("blink-yellow");

EDIT:

You can use this page for trying out such things... http://jsfiddle.net/rgkQK/3/

$(document).ready(function(){ 
 setInterval(findYellow,1000);     
 function findYellow(){ 
  $("tr.blinkYellow").each(function(){ 
       $(this).toggleClass("background-color-yellow");
     })
    }
}); 

In fiddle it looks like it would work quite well ;-)

Yves M.
Agreed, this would be a much better approach.
webfac
@Yves... Thank you very much, everything works as expected! The jsfiddle link was awesome..already bookmarked it. Before I used to create sample html markup using notepad++ but with this tool, everything is simple now... Thanks for helping newbies like us.. Keep it coming please!
Mark Estrada