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 9 of 9
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Javascript submit not working? [CODE]

    Code:
    <html>
    <head>
    <title>Translator</title>
    <script type="text/javascript">
    
    function checkOranges(numOranges)
       {
          if(numOranges == "hi")
             {
                alert("hai");
             }
    else if (numOranges == "lol")
    {
    alert("Laugh out loud");
    }
      
    
    </script>
    </head>
    <body>
    <h3>Internet abriviation translator</h3>
    <form method="POST" name="orangesform"
    onSubmit="checkOranges(document.orangesform.numOranges.value); return false;">
    <input type="text" name="numOranges" id=numOranges />
    <input type="Submit" name="Submit" />
    
    </form>
    
    
    
    </body>
    </html>
    when i run it no alert pops up after hitting submit when you type in the command (lol), does anyone know why? I can't figure out what is the issue with it.
    Last edited by hossaim; 12-06-2012 at 09:22 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Check for messages in error console or FireBug.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by WolfShade View Post
    Check for messages in error console or FireBug.
    I don't have firebug (I wrote this in notepad btw lol) and error console comes up with nothing.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Try a different browser then - all modern browsers except for Firefox have a debugger built in.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    function checkOranges(numOranges)

    <input type="text" name="numOranges" id=numOranges />

    Give the argument in the function a different id from the input.

    Put the id of the input in double quotes. ("")
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    * SIGH *

    This is just too simple.

    One very very minor fix needed:
    Code:
    <form method="POST" name="orangesform"
    onSubmit="checkOranges(document.orangesform.numOranges.value); return false;">
    There is NOTHING ELSE wrong in the original code.

    There are certainly things I would do differently. But that's a different question.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hossaim (12-06-2012)

  • #7
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    * SIGH *

    This is just too simple.

    One very very minor fix needed:
    Code:
    <form method="POST" name="orangesform"
    onSubmit="checkOranges(document.orangesform.numOranges.value); return false;">
    There is NOTHING ELSE wrong in the original code.

    There are certainly things I would do differently. But that's a different question.
    I did this, but upon hitting the submit there is still nothing happening.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You removed the last } from your function!

    You had that right before. Why did you change that?

    HINT: The number of { and } in any program must be the same.

    Ditto for [ and ].

    Ditto for ( and ).

    If you would indent your code sensibly you would see this.

    Code:
    <html>
    <head>
    <title>Translator</title>
    <script type="text/javascript">
    function checkOranges(numOranges)
    {
          if(numOranges == "hi")
          {
                alert("hai");
          }
          else if (numOranges == "lol")
          {
              alert("Laugh out loud");
          }
    }
    </script>
    </head>
    <body>
    <h3>Internet abriviation translator</h3>
    <form method="POST" name="orangesform"
    onSubmit="checkOranges(document.orangesform.numOranges.value); return false;">
    <input type="text" name="numOranges" id=numOranges />
    <input type="Submit" name="Submit" />
    
    </form>
    </body>
    </html>
    A better way to write this:
    Code:
    <html>
    <head>
    <title>Translator</title>
    <script type="text/javascript">
    var dictionary = {
        "hi" : "hai",
        "lol" : "Laugh Out Loud",
        "rotflmao" : "Rolling on the floor laughing my *** off",
        "fwiw" : "For what it's worth"
    };
    
    function dotranslate( btn )
    {
        var word = btn.form.translateFrom.value;
        btn.form.translateTo.value = dictionary[ word.toLowerCase() ];
    }
    </script>
    </head>
    <body>
    <h3>Internet abbreviation translator</h3>
    <form method="get">
    Abbreviation: <input type="text" name="translateFrom" /><br/>
    <input type="button" value="Translate!" onclick="dotranslate(this);" /><br/>
    Translation: <input type="text" name="translateTo" readonly /><br/>
    </form>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    hossaim (12-06-2012)

  • #9
    New Coder
    Join Date
    Dec 2012
    Posts
    11
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    You removed the last } from your function!

    You had that right before. Why did you change that?

    HINT: The number of { and } in any program must be the same.

    Ditto for [ and ].

    Ditto for ( and ).

    If you would indent your code sensibly you would see this.

    Code:
    <html>
    <head>
    <title>Translator</title>
    <script type="text/javascript">
    function checkOranges(numOranges)
    {
          if(numOranges == "hi")
          {
                alert("hai");
          }
          else if (numOranges == "lol")
          {
              alert("Laugh out loud");
          }
    }
    </script>
    </head>
    <body>
    <h3>Internet abriviation translator</h3>
    <form method="POST" name="orangesform"
    onSubmit="checkOranges(document.orangesform.numOranges.value); return false;">
    <input type="text" name="numOranges" id=numOranges />
    <input type="Submit" name="Submit" />
    
    </form>
    </body>
    </html>
    A better way to write this:
    Code:
    <html>
    <head>
    <title>Translator</title>
    <script type="text/javascript">
    var dictionary = {
        "hi" : "hai",
        "lol" : "Laugh Out Loud",
        "rotflmao" : "Rolling on the floor laughing my *** off",
        "fwiw" : "For what it's worth"
    };
    
    function dotranslate( btn )
    {
        var word = btn.form.translateFrom.value;
        btn.form.translateTo.value = dictionary[ word.toLowerCase() ];
    }
    </script>
    </head>
    <body>
    <h3>Internet abbreviation translator</h3>
    <form method="get">
    Abbreviation: <input type="text" name="translateFrom" /><br/>
    <input type="button" value="Translate!" onclick="dotranslate(this);" /><br/>
    Translation: <input type="text" name="translateTo" readonly /><br/>
    </form>
    </body>
    </html>
    This is like 70 times more efficient. Thank you a lot for your help!


  •  

    Posting Permissions

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