HTML flashing text colors

37,417

Solution 1

As per your request:

    function flashtext(ele,col) {
    var tmpColCheck = document.getElementById( ele ).style.color;

      if (tmpColCheck === 'silver') {
        document.getElementById( ele ).style.color = col;
      } else {
        document.getElementById( ele ).style.color = 'silver';
      }
    } 

    setInterval(function() {
        flashtext('flashingtext','red');
        flashtext('flashingtext2','blue');
        flashtext('flashingtext3','green');
    }, 500 ); //set an interval timer up to repeat the function

JSFiddle here: http://jsfiddle.net/neuroflux/rXVUh/14/

Solution 2

Here's an easy way to do it with plain JavaScript:

function flash() {
    var text = document.getElementById('foo');
    text.style.color = (text.style.color=='red') ? 'green':'red';
}
var clr = setInterval(flash, 1000);

This will alternate the color of the text between red and green every second. jsFiddle example.

Here's another example where you can set the colors of different elements:

function flash(el, c1, c2) {
    var text = document.getElementById(el);
    text.style.color = (text.style.color == c2) ? c1 : c2;
}
var clr1 = setInterval(function() { flash('foo1', 'gray', 'red') }, 1000);
var clr2 = setInterval(function() { flash('foo2', 'gray', 'blue') }, 1000);
var clr3 = setInterval(function() { flash('foo3', 'gray', 'green') }, 1000);

and the jsFiddle to go with it. You pass the ID of the element you want to flash and the two colors to alternate between.

Share:
37,417
user1022585
Author by

user1022585

Updated on September 06, 2020

Comments

  • user1022585
    user1022585 over 3 years

    This is quite an unusual request, but..

    Is there anyway to make some text alternate between 2 colors every second?

    So it appears as though it's flashing between say... red and grey? I don't mean background color, I mean the actual font color. I'm assuming it would need javascript or something.

    Is there any simple way to do that?

    (disregarding the fact it could look ugly)

    UPDATE

    Id like to call to this function several times on my page, each one passing along a different color to alternate with GREY

    .