0

I have the following HTML + JavaScript Code for Changing background color of HTML Page from JS based on the color option chosen. However irrespective of any label I choose, my background is turning red only on mouseover and white on mouseout. Rest of the color changes are not happening. Where am I going wrong?

<html>
    <head>
        <title>
        Color Highlighter
    </title>

<script type="text/javascript">
    function changeColor(target)
    {
         document.body.style.background = target;
    }

    function restoreColor()
    {
         document.body.style.background ="white";
    }
</script>

</head>

<body>
    <label onmouseover="changeColor('red');" onmouseout="restoreColor();">Red<br>
    <label onmouseover="changeColor('blue');" onmouseout="restoreColor();">Blue<br>
    <label onmouseover="changeColor('yellow');" onmouseout="restoreColor();">Yellow<br>
    <label onmouseover="changeColor('green');" onmouseout="restoreColor();">Green<br>
</body>

1
  • You neglected to close your label elements properly … Commented Feb 19, 2015 at 18:28

5 Answers 5

4

Your code is almost fine.

You forgot to complete </label> tag dude.

<label onmouseover="changeColor('red');" onmouseout="restoreColor();">Red</label><br>
<label onmouseover="changeColor('blue');" onmouseout="restoreColor();">Blue</label><br>
<label onmouseover="changeColor('yellow');" onmouseout="restoreColor();">Yellow</label><br>
<label onmouseover="changeColor('green');" onmouseout="restoreColor();">Green</label><br>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to close each <label> tag with </label>:

<label onmouseover="changeColor('red');" onmouseout="restoreColor();">Red</label><br>
<label onmouseover="changeColor('blue');" onmouseout="restoreColor();">Blue</label><br>
<label onmouseover="changeColor('yellow');" onmouseout="restoreColor();">Yellow</label><br>
<label onmouseover="changeColor('green');" onmouseout="restoreColor();">Green</label><br>

Comments

2

The problem is your HTML is invalid. You haven't closed your labels, so the first label contains all subsequent labels, and thus swallows the mouseover and mouseout events, preventing them from hitting the other labels.

Here's a demo of the problem, and how it can be solved:

HTML

<!-- fixed markup -->
<label class="valid" onmouseover="changeColor('red');" onmouseout="restoreColor();">Red</label>
<label class="valid" onmouseover="changeColor('blue');" onmouseout="restoreColor();">Blue</label>
<label class="valid" onmouseover="changeColor('yellow');" onmouseout="restoreColor();">Yellow</label>
<label class="valid" onmouseover="changeColor('green');" onmouseout="restoreColor();">Green</label>
<br/>
<br/>

<!-- original invalid markup -->
<label class="invalid" onmouseover="changeColor('red');" onmouseout="restoreColor();">Red<br>
<label class="invalid" onmouseover="changeColor('blue');" onmouseout="restoreColor();">Blue<br>
<label class="invalid" onmouseover="changeColor('yellow');" onmouseout="restoreColor();">Yellow<br>
<label class="invalid" onmouseover="changeColor('green');" onmouseout="restoreColor();">Green<br>

CSS

.valid {
    border:1px solid blue;
}

.invalid {
    border:1px solid red;
}

JavaScript

window.changeColor = function(target)
{
    document.body.style.background = target;
}

window.restoreColor = function()
{
    document.body.style.background ="white";
}

http://jsfiddle.net/5ufgg0uw/2/

demo

Using CSS, I've bordered the invalid labels with red and the valid labels with blue, so you can see the difference in behavior. It shows clearly how the first invalid label swallows up all following labels.

Comments

0

Why don't you just use changeColor('white') for onmouseout?

2 Comments

Since i have to change to white only onmouseout, how does it change anything to make function parameterized or not?
You know the changeColor function works - if you just use that method instead of creating a new one, it'll probably work. (I'm recommending you just use one function instead of two)
0

I think there's a problem with your markup. Why do none of the label tags close? The behavior of JavaScript event propagation up through a target element's ancestor nodes would cause the behavior you describe.

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.