Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing color using javascript

Tags:

javascript

i am a beginner to java script.i do the bellow code for changing the text color into red using java script.But it doesn't work.what is the error in my code?

<!DOCTYPE html>
<html>
<head>
<script>
function display()
{
var col=document.getElementById("demo").innerHTML;
col.style.color="red";
}
</script>
</head>
<body>

<h1>My First JavaScript</h1>
<p id="demo">click on the button bellow.....</p>

<button onclick="display()">Display</button>

</body>
</html> 
like image 696
DjangoDev Avatar asked Jan 29 '13 04:01

DjangoDev


2 Answers

Remove innerHTML from var col=document.getElementById("demo").innerHTML;

<!DOCTYPE html>
<html>
<head>
<script>
function display()
{
var col=document.getElementById("demo");
col.style.color="#FF0000";
}
</script>
</head>
<body>

<h1>My First JavaScript</h1>
<p id="demo">click on the button below.....</p>

<button onclick="display()">Display</button>

</body>
</html>
like image 149
Prasanth Bendra Avatar answered Oct 13 '22 05:10

Prasanth Bendra


Dont use the innerHTML, it returns a String.

Use the style on the object itself.

Check out it working: JsFiddle

like image 29
Felipe Fonseca Avatar answered Oct 13 '22 06:10

Felipe Fonseca