1
<html>
    <head>
        <script language="Javascript">
            function changecolor(var c)
            {
            document.body.style.background=c;
            }
        </script>
    </head>
    <body>
        <table width="300" height="100" align="center" border=2>
            <tr>
                <td onmouseout=changecolor("transparent") onmouseover=changecolor("red")>red</td>
                <td onmouseout=changecolor("transparent") onmouseover=changecolor("green")>green</td>
                <td onmouseout=changecolor("transparent") onmouseover=changecolor("blue")>blue</td>
            </tr>
        </table>
    </body>
</html>

I am not getting the change of color. Can someone please help...! Thanks in advance.

5
  • please use jsfiddle.net to show your example Commented Aug 17, 2013 at 7:07
  • 3
    functions don't have var in them function changecolor(var c) should be function changecolor(c) Commented Aug 17, 2013 at 7:08
  • 2
    You should surround your onmouseout/onmouseover statements with ''. That is, onmouseout=changecolor("transparent") should be onmouseout='changecolor("transparent")' Commented Aug 17, 2013 at 7:11
  • Why so many identical answers ? When one is made, no need to add other ones... Commented Aug 17, 2013 at 7:47
  • @dystroy let just close this post as we know OP got the answer. Commented Aug 17, 2013 at 7:49

5 Answers 5

2

You should write

function changecolor(c)

instead of

function changecolor(var c)

Because when you write var c in the function declaration, the browser will declare a new variable in the scope of your function. So when you then use the c variable it will return undefined since you didn't assigned some data to the variable.

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

Comments

0

In ur javascript functions no need to declare type of parameters

so change

function changecolor(var c)

to

function changecolor(c)

this will solve ur problem.

Comments

0
<html>
<head>
    <script language="Javascript">
        function changecolor(c)
        {
            document.body.style.backgroundColor=c;
        }
    </script>
</head>
<body>
    <table width="300" height="100" align="center" border=2>
        <tr>
            <td onmouseout="changecolor('white')" onmouseover="changecolor('red')">red</td>
            <td onmouseout="changecolor('white')" onmouseover="changecolor('green')">green</td>
            <td onmouseout="changecolor('white')" onmouseover="changecolor('blue')">blue</td>
        </tr>
    </table>
</body>

Comments

0

Try this:

 <script language="Javascript">
            function changecolor(c)//remove var
            {
            document.body.style.background=c;
            }
        </script>

Comments

0
<body>
<table width="300" height="100" align="center" border=2>
    <tr>
        <td id="redcolor" onmouseover="change_red()" onmouseout="change_color()">red</td>
    <td id="greencolor" onmouseover="change_green()" onmouseout="change_color()">green</td>
        <td id="bluecolor" onmouseover="change_blue()" onmouseout="change_color()">blue</td>
    </tr>
 </table>

 <script>  
     function change_red()
         {
             document.getElementById("redcolor").style.color="red";
         }

     function change_green()
        {
     document.getElementById("greencolor").style.color="green";
 }

function change_blue()
{
    document.getElementById("bluecolor").style.color="blue";
}

function change_color()
{
document.getElementById("redcolor").style.color="#000";
document.getElementById("greencolor").style.color="#000";
document.getElementById("bluecolor").style.color="#000";
}
</script>  

</body>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.