celloplayer
celloplayer

Reputation: 31

SyntaxError when trying to get style

I'm trying to make a function that fades an object's color to another color in Javascript for a link, but when I use Firebug's JavaScript console, the console gives me a SyntaxError:

SyntaxError: syntax error
[Break On This Error] if (document.getElementById(fadeID).style.color = "#E6E6E6")){ 

The Javascript (I was using alert to test if it was working. If I put it outside the if l:

<script>
    function toggleColor(fadeID){
        if (document.getElementById(fadeID).style.color = "#E6E6E6")){
            alert("hi");
        }
    }
</script>

The HTML:

<body>
<div id = "content">
    <div id = "header">
        <a onmouseover = "javascript:toggleColor('home')" id = "home" href = "#" title = "Home">Home</a> |
        <a onmouseover = "javascript:toggleColor('music')" id = "music" href = "#" title = "Music">Music</a>
        </h1>
    </div>
</div>

Upvotes: 0

Views: 66

Answers (3)

Black Sheep
Black Sheep

Reputation: 6694

you have to remove a clamp and equal sign:

if (document.getElementById(fadeID).style.color = "#E6E6E6")){

to

if (document.getElementById(fadeID).style.color == "#E6E6E6"){ // Here

Upvotes: 0

Musa
Musa

Reputation: 97682

You have an extra ) in your if statement also you are assigning a value instead of comparing it

if (document.getElementById(fadeID).style.color == "#E6E6E6"){

Upvotes: 1

Gurminder Singh
Gurminder Singh

Reputation: 1755

 if (document.getElementById(fadeID).style.color = "#E6E6E6")){

should be

 if (document.getElementById(fadeID).style.color == "#E6E6E6"){

You are assigning the color with =, not comparing. See FIDDLE

Upvotes: 5

Related Questions