Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2012, 05:39 PM   PM User | #1
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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..
hossaim is offline   Reply With Quote
Old 12-06-2012, 05:43 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 12-06-2012, 05:45 PM   PM User | #3
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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.
hossaim is offline   Reply With Quote
Old 12-06-2012, 06:32 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,462
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-06-2012, 07:16 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 12-06-2012, 07:39 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
* 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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
hossaim (12-06-2012)
Old 12-06-2012, 08:04 PM   PM User | #7
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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.
hossaim is offline   Reply With Quote
Old 12-06-2012, 08:25 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
hossaim (12-06-2012)
Old 12-06-2012, 09:21 PM   PM User | #9
hossaim
New Coder

 
Join Date: Dec 2012
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
hossaim is an unknown quantity at this point
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!
hossaim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:07 AM.


Advertisement
Log in to turn off these ads.