ms_27
ms_27

Reputation: 1704

How to change color of text when it is clicked

I want to change the color of text when clicking on it. Can anybody let me know why this code isnt working?

<html>
<body>
 <center>
  <div id="web" style="color:black" onclick="changeformat(this.id)"> Web </div>
  <div id="img" style="color:blue" onclick="changeformat(this.id)"> Images </div>
  <div id="news" style="color:blue" onclick="changeformat(this.id)"> News </div>
 </center>
</body>
</html>

<script>
function changeformat(type)
    {
    document.getElementById('web').style = "color:blue;";
    document.getElementById('img').style = "color:blue";
    document.getElementById('news').style = "color:blue";  
    document.getElementById(type).style = "color:black";
    }
</script>

Upvotes: 0

Views: 321

Answers (5)

Krishnaram
Krishnaram

Reputation: 11

try this code

function changeformat(type)
    {
    document.getElementById('web').style.color = 'green'; 
    document.getElementById('img').style.color = 'pink'; 
    document.getElementById('news').style.color = 'red'; 
    document.getElementById(type).style.color = "black";
    }

Upvotes: 0

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94409

You have to set the color this way:

//element.style.CSSProperty = Value;
  ele.style.color = "blue";

An optimized version:

<div id="web" style="color:black" onclick="changeformat(event)"> Web </div>

function changeformat(e){
    var eles = document.querySelectorAll("div");
    for(var i = 0 ; i < eles.length; i++){
        eles[i].style.color = "blue";
    }
    e.target.style.color = "black";
}

Demo: http://jsfiddle.net/DerekL/V378R/2/

Upvotes: 0

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

<div id="web" style="color:black" onclick="changeformat(this)"> Web </div>
<div id="img" style="color:blue" onclick="changeformat(this)"> Images </div>
<div id="news" style="color:blue" onclick="changeformat(this)"> News </div>

function changeformat(element) {
    var elements = ['web', 'img', 'news'];
    for( var i = 0, length = elements.length; i < length; i++  ) {
        document.getElementById( elements[i] ).style.color = 'blue';
    }
    element.style.color = "black";
}

Demo

Upvotes: 0

Daniel Imms
Daniel Imms

Reputation: 50269

You almost had it, use element.style.color

jsFiddle

function changeformat(type) {
    document.getElementById('web').style.color = "blue;";
    document.getElementById('img').style.color = "blue";
    document.getElementById('news').style.color = "blue";  
    document.getElementById(type).style.color = "black";
}

As Derek points out you can also use element.setAttribute(), this will override other inline styles that are already set on the element though.

jsFiddle

function changeformat(type) {
    document.getElementById('web').setAttribute("style", "color:blue;");
    document.getElementById('img').setAttribute("style", "color:blue");
    document.getElementById('news').setAttribute("style", "color:blue");
    document.getElementById(type).setAttribute("style", "color:black");
}

Upvotes: 1

Sharif Ahmed
Sharif Ahmed

Reputation: 26

Try This, It will work. The correct way to change the color is using: document.getElementById(id).style.color

<html>
<body>
 <center>
   <div id="web" style="color:black" onclick="changeformat(this.id)"> Web </div>
   <div id="img" style="color:blue" onclick="changeformat(this.id)"> Images </div>
   <div id="news" style="color:blue" onclick="changeformat(this.id)"> News </div>
 </center>
</body>
</html>

<script>
function changeformat(type)
{
document.getElementById('web').style.color = "blue";
    document.getElementById('img').style.color = "blue";
    document.getElementById('news').style.color = "blue";  
    document.getElementById(type).style.color = "black";
}
</script> 

Upvotes: 1

Related Questions