-1

I am trying to write basic JavaScript which changes the background of a paragraph to yellow and then to pink on click.

<p id="foo">Hello, people!</p>

and script is:

window.onload = function(){
var foo = document.getElementById("foo");
foo.onclick = function(){
        if(foo.style.background!=="yellow")foo.style.background = "yellow";
        if(foo.style.background === "yellow")  foo.style.background = "pink";
};
};

The color changes to yellow on first click but it does not change to pink when I click again. I can't figure out the problem.

5
  • 1
    What does this have to do with jQuery? Commented Oct 25, 2011 at 17:55
  • Easiest thing is to stick an alert(foo.style.background) or a console.log(foo.style.background) in your onclick function to see what the value is each time. Commented Oct 25, 2011 at 17:56
  • 1
    Internally, the browser may be using a different representation than the string "yellow" to denote the background. console.log(foo.style.background) to see what it actually is. Commented Oct 25, 2011 at 17:56
  • @Tom , @Michael using console.log returned this: none repeat scroll 0% 0% yellow Commented Oct 25, 2011 at 18:14
  • while in Chrome console.log simply returned yellow This seems to be the problem. Commented Oct 25, 2011 at 18:16

4 Answers 4

7

EDIT: Don't use background, use backgroundColor

Fixed example

    if(foo.style.background!=="yellow")foo.style.background = "yellow";
    if(foo.style.background === "yellow")  foo.style.background = "pink";

needs to be:

    if(foo.style.background!=="yellow")foo.style.background = "yellow";
    else if(foo.style.background === "yellow")  foo.style.background = "pink";

because you changing it to yellow, then checking if it's yellow and making it pink

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

7 Comments

Well, here's one more reason to use curly braces, line breaks and indentation with if/else blocks.
No. It does not work even with 'else'. Plus even without else it should do same thing.
@Jatin: No, it shouldn't. You need to add the else and still keep it there, if anything.
@BoltClock Yes, curly braces and indentation should have been used but that does not seem to be the problem.
Because firefox addes other values, none repeat scroll 0% 0% yellow
|
0

Try changing

if(foo.style.background === "yellow") 

to this

if(foo.style.background == "yellow") 

Comments

0

It might be due to the way you're doing your comparison:

foo.onclick = function(){
        if(foo.style.background != "yellow") foo.style.background = "yellow";
        if(foo.style.background == "yellow") foo.style.background = "pink";
};

Note the use of "!=" and "==" instead of "!==" and "===".

Comments

0

Try:

var foo = document.getElementById("foo");
foo.onclick = function(){
        if(foo.style.background!="yellow")foo.style.background = "yellow";
        else if(foo.style.background =="yellow")  foo.style.background = "pink";
};

http://jsfiddle.net/AvKPE/

var foo = document.getElementById("foo");
foo.onclick = function(){
        if(foo.style.backgroundColor!="yellow")foo.style.backgroundColor = "yellow";
        else if(foo.style.backgroundColor =="yellow")  foo.style.backgroundColor = "pink";
};

http://jsfiddle.net/AvKPE/2/

1 Comment

Ok the problem is that firefox returns the whole background property: 0% 0% fixed ... yellow ... Try new link: jsfiddle.net/AvKPE/2

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.