0

I've seen all the posts but my problem isn't that it doesn't change the background color but rather that it does and the change it but to the original. Here's the code.

    <html>
        <head>
            <title>Calculator</title>
            <link rel="stylesheet" type="text/css" href="color.css"/>
            <script type="text/javascript">
                function calc(color){
                    //document.body.bgColor = color;
                   x=document.myForm
                   var val1=x.input1.value;
                   var val2=x.input2.value;
                   var val3=val1+"+"+val2;
                   if(eval(val3)<0) return;
                    else{

                   alert("The result is "+eval(val3));
                    var bd = document.getElementById ('body');
                    bd.className="highlight";
                    return;
                    }
                }
            </script>
        </head>
        <body id="body" onload="changeBackground(red)">
            <form name="myForm" onsubmit="calc(this)">
                Enter 2 values to add ;)
                <input type="text" name="input1">
                <input type="text" name="input2">
                <input type="submit" value="Send Input">
            </form>
        </body>
    </html>

CSS:

    .hightlight{
       background-color: yellow;
    }

I understand that the function ends right before the end of the function. So how can I get it to hold the color?

2
  • 4
    If someone answers, is there any chance you'll accept it? I'm guessing no... Commented Feb 20, 2010 at 14:08
  • you need to accept others' answers to increase your reputation of acceptance score. Currently your acceptance percentage is 0%. Commented Feb 20, 2010 at 14:09

1 Answer 1

1

The problem is that you tries to POST page over submit button. In this case page is fully redrawn, without keeping previous state that is set by your script. Just change code of button:

<input type="button" value="Send Input" onclick="calc(this)">
Sign up to request clarification or add additional context in comments.

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.