0

I am trying to change the text color of a <span> tag.

<script type="text/jscript">
window.setInterval(function(){
    if ($("#sw").css("color") == "red") {
    $("#sw").css("color","blue");
    }
    else if($("#sw").css("color") == "blue") {
    $("#sw").css("color","green");
    }
    else if($("#sw").css("color") == "green") {
    $("#sw").css("color","red");
    }
    }, 1000);


 </script> 

The above example should change the text color within the <span> tag.

I have tested it in Internet Explorer and it works fine, but it doesn't work in Google Chrome or Mozilla Firefox.

What is the problem? Do these browsers not support this? Are there any workarounds? What is the solution for this issue?

Corion
  • 663
  • 3
  • 14
Mahesh Wagh
  • 151
  • 1
  • 13
  • This has nothing to do with C# or ASP.net. – crush Jul 16 '13 at 12:53
  • browser vyawasthit aahe na? [javascript enabled???] – C Sharper Jul 16 '13 at 12:54
  • @M.N.S., means?. If you have doubt then you may check it and find out your self but in my case it is not works. – Mahesh Wagh Jul 16 '13 at 13:08
  • @MaheshWagh dont u understand marathi? – C Sharper Jul 16 '13 at 13:09
  • 1
    @M.N.S. Yes, but it's a Global Site and better to use global language as anybody can understand it. – Mahesh Wagh Jul 16 '13 at 13:25
  • The [jQuery Color Plugin](https://github.com/jquery/jquery-color) may solve your issue. – Corion Jul 16 '13 at 14:37
  • @MaheshWagh thanks for "Teaching" me SO rules. – C Sharper Jul 17 '13 at 05:08
  • @Corion, Thanks for your suggestion buddy but I have a doubt that's It's Installing on local server and may be works on it but what about when I upload the file on host server on Net Is it enough to works by only references there?. Please reply for my knowledge?. – Mahesh Wagh Jul 17 '13 at 05:51
  • If it works locally but not on the site (regardless of browser) make sure that jQuery is being loaded properly. It shouldn't make a difference with HTML/JavaScript where the files are as long as they're being loaded. – Corion Jul 17 '13 at 14:08

2 Answers2

0

check this fiddle Link

 setInterval(function(){
  if($("#sw").css("color") == "rgb(0, 0, 255)"){
   $("#sw").css("color","Red");
  }
  else if($("#sw").css("color") == "rgb(255, 0, 0)")
  {
    $("#sw").css("color","green");
  }
  else{
   $("#sw").css("color","Blue");
  }
 },1000);
Kathiravan
  • 689
  • 1
  • 7
  • 22
0

You're getting the RGB value rather than the color name

when i used var look = $('#sw').css('color'); the result was "rgb(255, 0, 0)" in the chrome debugger

Your code isn't working because the string returned is the rgb value of colour, rather than the color itself, replacing the names with RGB values fixeds your code.

 <script type="text/jscript">
                        window.setInterval(function () {
                    if ($('#sw').css('color') === 'rgb(255, 0, 0)') {
                        $("#sw").css("color", "rgb(0, 0, 255)");
                        }
                    else if ($("#sw").css("color") === "rgb(0, 0, 255)") {
                        $("#sw").css("color", "rgb(0, 128, 0)");
                        }
                    else if ($("#sw").css("color") === "rgb(0, 128, 0)") {
                         $("#sw").css("color", "rgb(255, 0, 0)");
                        }
                    }, 1000);
 </script> 

As noted in the second related question, this won't worrk on IE. because IE uses hex values. I suggest using another else statement to account for that. something like

else if ($("#sw").css("color") === "#FF0000") {
                        $("#sw").css("color", "#00FF00");
                        }

either that or using an or condition on your basic statements like

 if ($('#sw').css('color') === 'rgb(255, 0, 0)')||($("#sw").css("color") === "#FF0000") {
                    $("#sw").css("color", "rgb(0, 0, 255)");
                    }

I imagine that it works in Firefox because firefox will store the css color property however you specify it, however chrome will normalise the css into an RGB value, though i may be wrong

Also, you may be able to make it more efficient by using straigt javascript. something like: How to get the background color of an element using javascript?

Related Questions:

Jquery if then statement for css value

How to get hex color value rather than RGB value?

Community
  • 1
  • 1
  • There are certain error in string quoting like " instead of '. It is works in google chrome but not in Internet Explorer. – Mahesh Wagh Jul 16 '13 at 13:46
  • as per your suggestion the string return RGB Value rather then color itself so why the many languages utilise the color name instead of RGB Value?. – Mahesh Wagh Jul 16 '13 at 13:48
  • Yes, if you look in the question about converting RGB to hex, IE returns a hex value, so you need to account for that – Alexander Troup Jul 16 '13 at 14:09
  • It's not different languages using different conventions, it's different browsers. I imagine they each normalise the data so that whatever format you give their code can interpret it, so instead of having 3 handlers for color: red, #FF0000 and rgb(255,0,0) they have a converter that sets them all to the same one then handles that case. you may be better going with javascript directly rather than Jquery. – Alexander Troup Jul 16 '13 at 14:24