Connecting Tech Pros Worldwide Forums | Help | Site Map

Calling a function

Member
 
Join Date: Aug 2008
Posts: 49
#1: Sep 3 '08
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>

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#2: Sep 3 '08

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
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#3: Sep 3 '08

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