Reputation: 1704
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
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
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
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";
}
Upvotes: 0
Reputation: 50269
You almost had it, use element.style.color
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.
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
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