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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Making text box connected to Button

    Hi, this is my first time doing Javascript and I tried to create a small word game but it didn't come out right. In the example, the word "happy" is suppose to be the correct word and if the person guesses it correctly an alert box would pop up telling that person that they got it correct. But when I test it out nothing pops up. I think what's happening is that the text box is not really connected to the "Enter" button so the button does not work accordingly. So is there any way to fix this? Here is the code...

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function unscramb()
    {
    var x = document.forms['form1'].happy.Enter;
    x="happy";
    if (x==happy);
     {
    alert("Great! You got it!");
     }
    else
     {
    alert("Sorry, try again!");
     }
    }
    </script>
    </head>
    <body>
    <form name="form1">
    Unscramble the word. <input type="text" size=5 maxlength=5 name="happy"> 
    <input type="button" onclick="unscramb()"
    value="Enter">
    </form>
    </body>
    </html>
    Thanks for any help!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function unscramb() {
    var x = document.form1.happy.value;
    if (x.toLowerCase() == "happy") {    // make case insensitive
    alert("Great! You got it!");
    }
    else {
    alert("Sorry, try again!");
    document.form1.happy.value = "";    // clear the box
    document.form1.happy.focus();    // and focus on it again
    }
    }
    </script>
    </head>
    <body>
    <form name="form1">
    Unscramble the word. <input type="text" size=5 maxlength=5 name="happy"> 
    <input type="button" onclick="unscramb()" value="Enter">
    </form>
    </body>
    </html>

    A child of five would understand this. Send someone to fetch a child of five.
    Groucho Marx
    Last edited by Philip M; 08-25-2008 at 08:51 PM.

  • Users who have thanked Philip M for this post:

    Jolly Rancher (08-25-2008)

  • #3
    New Coder
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    86
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Try this!

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function unscramb() {
    
    x = document.form1.happy.value
    
    if (x == "happy") {
    
    alert("Great! You got it!")
    
    }
    else if(x != "happy") {
    
    alert("Sorry, try again!")
    
    }
    }
    </script>
    </head>
    <body>
    <form name="form1">
    Unscramble the word. <input type="text" size=5 maxlength=5 name="happy"> 
    <input type="button" onclick="unscramb()"
    value="Enter">
    </form>
    </body>
    </html>
    "The one closer to the truth is the one still learning"

  • Users who have thanked Jodarecode for this post:

    Jolly Rancher (08-25-2008)


  •  

    Posting Permissions

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