0

So I am trying to make something where whenever I click on the text it change's color.

Javascript:

function changecolor(){
    var tc = document.getElementById("header").style.color.value;
    if  (tc = "#000000") { tc = "#0009FF"}
    else if (tc == "#0009FF") { tc = "#FF0000"}
    else if (tc == "#FF0000") { tc = "#15FF00"}
    else if (tc == "#15FF00") { tc = "#FFA600"}
    else {tc = "#000000"};
    document.getElementById("header").style.color.value = tc;
}

html:

<div onclick="changecolor()"><h1 id="header" style="color:#000000;"> Nick's Basic Physic's Calculator </h1></div>

It is not working and I have not been able to figure out why. When I click on the text nothing happens.

1
  • Not sure, but I think that problem in your html onclick="changecolor()" because I think that on click will be executed result of changecolor function, try to change it to onclick="changecolor" Commented Oct 13, 2013 at 22:35

3 Answers 3

1

Change

document.getElementById("header").style.color.value = tc;

to

document.getElementById("header").style.color = tc;

Working example:

http://jsfiddle.net/xEyLf/

Sign up to request clarification or add additional context in comments.

Comments

1

your problem is in var tc = document.getElementById("header").style.color.value;

you have to change to tc = document.getElementById("header").style.color;in order to get the color into variable.

Comments

0

I have another solution... here's a jsfiddle: http://jsfiddle.net/9zfJA/

Keep in mind I'm using these jQuery methods (as well as the jQuery library itself), by simply switching between CSS classes:

hasClass(): to check if the class is present

addClass(): to add the correct class based on the condition above

removeClass(): remove all classes, I've basically built a "reset" function

I think there are errors in the way it functions and how you want it, but I've built it on the same logic you have in your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.