Connecting Tech Pros Worldwide Help | Site Map

Calling a function

  #1  
Old September 3rd, 2008, 07:58 PM
Member
 
Join Date: Aug 2008
Posts: 49
I am trying to click a button, call a javascript function with a variable and then parse the result. At this point it is not doing anything.

What am I doing wrong?


Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3. function makeBread(abc) {
  4.     If (String(abc)=="1") {
  5.         alert("Bread's Done!:1");
  6.         }
  7.     If (String(abc)=="2") {
  8.         alert("Bread's Done!:2");
  9.         }
  10. }
  11. </script>
  12.  
  13. <body>        
  14. <input type="button" onClick="javascript:makeBread('1');" value="Done">    
  15. </body>

Last edited by gits; September 3rd, 2008 at 09:34 PM. Reason: fix code tags usage
  #2  
Old September 3rd, 2008, 09:00 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,490
Provided Answers: 10

re: Calling a function


I would omit the String() function since datatypes are converted as you need them.

maybe you want to use switch() instead of 2 if's.

what about an event handler like addEventListener()/attachEvent()?

regards
  #3  
Old September 3rd, 2008, 09:45 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,102
Provided Answers: 1

re: Calling a function


1. javascript is case-sensitive and therefor:

Expand|Select|Wrap|Line Numbers
  1. If 
must be

Expand|Select|Wrap|Line Numbers
  1. if
otherwise you will get an error because the interpreter 'thinks' you are calling a custom function If() since it is not a keyword.

now it should work with the change, but:

2. the language attribute is deprecated so you should use:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
and

3. the following:

Expand|Select|Wrap|Line Numbers
  1. <input type="button" onClick="javascript:makeBread('1');" value="Done"> 
  2.  
should be:

[HTML]<input type="button" onclick="makeBread('1');" value="Done">
[/HTML]
according to the specs.

kind regards
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print function name of calling function? dspfun answers 15 February 21st, 2007 03:35 AM
returning control back to calling function sumanthsclsdc answers 2 November 16th, 2006 06:58 PM
Calling function name Saroj answers 16 November 14th, 2005 11:28 PM
Addin calling function in Parent jchao123@hotmail.com answers 6 November 13th, 2005 04:41 AM
Altering the namespace of the calling function Andrew Wilkinson answers 1 July 18th, 2005 05:34 AM