Question

I am wondering if my jquery function is correct

<script>
window.blinker = setInterval(function(){
  if(window.alerta){
    $('a.cadastrotopo').css('color','#346698');
    $('a.cadastrotopo').css('text-decoration','underline');
      window.alerta=false;
    }
    else{
      $('a.cadastrotopo').css('color','#000');
      $('a.cadastrotopo').css('text-decoration','none');
      window.alerta = true;
    }
},500);
</script>

is working ok, but I wonder if I'm doing the right way.

I thank you.

Was it helpful?

Solution

Personally I would use CSS more, particularly classes:

a.cadostropo {
  color: #000;
  text-decoration: none;
}
a.alert { 
  color: #346698;
  text-decoration: underline;
}

and then the solution becomes trivial:

setInterval(toggleAlert, 500);

function toggleAlert() {
  $("a.cadostropo").toggleClass("alert");
}

One side note: instead of multiple css() calls you can use anonymous objects to specify multiple properties.

$("a.cadostropo").css({color: "#346698", textDecoration: "underline"});

That being said, I prefer not to use hardcoded CSS manipulation like this. Favour classes. They're far more flexible and you don't have to worry about destructive changes and rolling them back.

OTHER TIPS

Yes, you are, although you could combine the css declarations to look like this.

.css({'color' : "#346698", 'text-decoration' : "underline"});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top