1

I'm trying to change the background color of a table when the mouse hovers it. How do I fix it?

I get this error: "Unexpected token ERROR" in Google Chrome

    echo '<table id="a1" onmouseover="document.getElementById(\'a1\').backgroundColor = #CCCCCC;">
    <td align="center"></td></table>';

What am I doing wrong?

8
  • 3
    this could just be done in css... using #a1:hover{background-color:#CCC;} Commented Aug 14, 2011 at 3:34
  • 1
    You may want to consider doing this via CSS or jQuery. Echoing out a whole bunch of js events won't be very efficient or easy to maintain. If you have questions about those choices just ask! Commented Aug 14, 2011 at 3:34
  • @mrtsherman, jQuery is also Javascript and even if he has to do this with JS, jQuery is overkill. Commented Aug 14, 2011 at 3:44
  • @omeid - I am aware that jQuery is javascript =). He can write this in a single line of jQuery. Or he could write a lot of javascript or a lot of redundant javascript. Why not use tools that make your development fast and easy? Of course the whole thing is moot since the real answer is CSS. Commented Aug 14, 2011 at 7:31
  • @mrtsherman: your fast development shouldn't cost resources. Commented Aug 14, 2011 at 11:40

4 Answers 4

2

#CCCCCC should be a string, \'#CCCCCC\'

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

Comments

1
function changeBackground(color) {
   document.getElementById('a1').backgroundColor = color;
}

<table id="a1" onmouseover="changeBackground('red');">

Comments

1

Try this.

echo '<table id="a1" onmouseover="this.style.backgroundColor=\'#ccc\'" onmouseout="this.style.backgroundColor=\'#000\'">
<td align="center"></td></table>';

Because,

  1. backgroundColor is a property of style property of the HTMLElement.
  2. #CCCCCC should be in quotes

An ideally you should use the powerful this keyword in scenarios like this.

1 Comment

Thanks! Now how do I change the background color to nothing? Like - no background color at all? I tried putting nothing in the color string - but it didn't work.
0

Use CSS to pull this off, it will run faster and look nicer (you won't get any screwy edge cases).

#a1 {
    background-color: #FFF;
}

#a1:hover {
    background-color: #CCC;
}

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.