Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2003
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Missing ) after condition?

    Hey all

    I have been asked to write a js prog/util for an after-school program at my church. It's pretty simple: just read the problem, type in the answer,
    and click a button to see if it's right.

    My problem is this:

    I have written the basic structure for this prog the way I always have,
    except now it's not working. I ran it through the netscape javascript console
    and I got the following error:
    Code:
    function eval()
    {
    if (document.ans.2t2.value="4");
    alert("Correct");
    }
    Missing ) after condition.


    I cannot see what is wrong with this script.
    I feel kind of dumb b/c conditons are usually very easy and I never have
    problems with them.

    Here is my full souce:
    Code:
    <html>
    <head>
    <script language="javascript">
    
    function eval()
    {
    if (document.ans.2t2.value =="4") ;
    alert("2x2=4. Correct.");
    }
    
    </script>
    </head>
    <body>
    <form name="ans">
    2x2 <input type="textarea" name="2t2"><br>
    <input type="button" value="Eval" onclick="eval()"></form>
    </body>
    </html>
    Can anyone see what is wrong with this script?
    Any help is appreciated.
    Carl
    Last edited by flare1028us; 06-23-2004 at 12:17 AM. Reason: just fixing some tags

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Names or ID's can not begin with a numeric. Change your input name and do not use eval as a function name since there is already a javascript eval method and therefore it is a reserved word.

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function eval() {
         if (document.ans.2t2.value="4");
         alert("Correct");
    }
    Get rid of the ";".

    So that it looks like (With Willy's suggestion)
    Code:
    function evaluateAnswer() {
         if (document.ans.2t2.value="4")
              alert("Correct");
    }

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You should use the conditional operator == not the equality operator =
    And if you still want to name your field starting with numbers, use the bracket notation.

    Code:
    function evaluateAnswer() {
         if (document.ans.elements['2t2'].value=="4")
              alert("Correct");
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Nov 2003
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thankx 4 the help

    lol now I just feel dumb!

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    And if you still want to name your field starting with numbers, use the bracket notation.
    I was not aware of that.

    Thanks;
    .....Willy

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Willy Duitt
    I was not aware of that.

    Thanks;
    .....Willy
    You should be by now.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •