Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 3rd, 2008, 07:58 PM
Member
 
Join Date: Aug 2008
Posts: 43
Default Calling a function

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
Reply
  #2  
Old September 3rd, 2008, 09:00 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 893
Default

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
Reply
  #3  
Old September 3rd, 2008, 09:45 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 36
Posts: 3,448
Default

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.