www.webdeveloper.com
Results 1 to 7 of 7

Thread: passing multiple variables to js function

  1. #1
    Join Date
    Jul 2012
    Posts
    4

    Lightbulb passing multiple variables to js function

    Hello, my English is not at his best but I will try...

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function myFunction(var1,var2)
    {
    alert(var1 + " & " + var2);
    }
    </script>
    </head>
    
    <body>
    Username:&nbsp;&nbsp;<INPUT Type="Text" Maxlength=11 Name="UsernameField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Username"/>&nbsp;
    Pass:&nbsp;<INPUT Type="Password" Maxlength=11 Name="PasswordField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Password" /> <BR>
    <button onclick="myFunction(form.UsernameField.value,form.PasswordField.value)">Login</button>
    
    <p>By clicking the button above, a function will be called. The function will alert a message.</p>
    
    </body>
    </html>
    Like you can see, i pass 2 variables to a js function and with that function, i will display them, something goes wrong, you know?

    Thank you in advance

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    The "form" variable has not been defined anywhere. Additionally, you do not have a <form> element in your page. Start by wrapping your form fields and buttons in <form> tags, then change your function call to:

    myFunction(this.form.UsernameField.value, this.form.PasswordField.value);

    When the onclick executes, "this" points to the <button> tag, which has a form property pointing to the form that contains the button.

  3. #3
    Join Date
    Jul 2012
    Posts
    4
    WOW, it works, thank you very mutch!!!

  4. #4
    Join Date
    Jul 2012
    Posts
    4
    New Problem, i tested on a new html page and it works, but now i putted it in my page, i can''t get it work, here is the code for the input boxes:

    HTML Code:
    <DIV Class="transbox"><P>
    <DIV Align="Right">
    <Form Name="Login">
    Username:&nbsp;&nbsp;<INPUT Type="Text" Maxlength=11 Name="UsernameField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Username"/>&nbsp;
    Pass:&nbsp;<INPUT Type="Password" Maxlength=11 Name="PasswordField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Password" /> <BR>
    </Form>
    <FORM NAME="this">
    Onthoud:&nbsp;&nbsp;<INPUT TYPE="checkbox" NAME="check1" VALUE="Check1">Username&nbsp;&nbsp;
    &nbsp;&nbsp;en/of&nbsp;&nbsp;&nbsp;<INPUT TYPE="checkbox" NAME="check2" VALUE="Check2">Password&nbsp;&nbsp;&nbsp;&nbsp;<BR>
    <A Href="">Password vergeten?</A>&nbsp;&nbsp;<INPUT TYPE="button" VALUE="Click" onClick="testButton(this.form, this.form.UsernameField.value, this.form.PasswordField.value)">
    </FORM></DIV></P></DIV>

    And here is the code for the function:

    HTML Code:
    <SCRIPT Language="Text/JavaScript">
    function testButton(form, Username, Password) {
         var alertStr = "";
    
         for (Count = 0; Count < 2; Count++) {
              if (form[Count].checked) alertStr += "CheckBox " + (Count + 1) + " is checked";
         }
    
         // if (alertStr == "") alertStr = "No CheckBox was selected";
         if (!alertStr) alertStr = "No CheckBox was selected";
    
         alert(alertStr);
    	 if (alertStr == "No CheckBox was selected")
    	 {
    	 }
    	 else if(alertStr == "CheckBox 1 is checkedCheckBox 2 is checked")
    	 {
    		alert(Username + " & " + Password)
    	 }
    	 else if(alertStr == "CheckBox 1 is checked")
    	 {
    		alert("Username: " + Username)
    	 }
    	 else if(alertStr == "CheckBox 2 is checked")
    	 {
    		alert("Password: " + Password)
    	 }
    }
    
    </SCRIPT>
    Please help me with my last question!!!!!

  5. #5
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773

  6. #6
    Join Date
    Jul 2012
    Posts
    4

    Error

    Just when i click at the button, nothing happends, and i don't get a javascript error, just nothing happends, while when i only send 1 variable to a function, everything goes fine, try this code yourself and you will probably see!

  7. #7
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    The <input>s and <button> need to be inside the same <form>

    Code:
    <DIV Class="transbox">
    	<P>
    		<DIV Align="Right">
    			<Form Name="Login">
    				Username:&nbsp;&nbsp;<INPUT Type="Text" Maxlength=11 Name="UsernameField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Username"/>&nbsp;
    				Pass:&nbsp;<INPUT Type="Password" Maxlength=11 Name="PasswordField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Password" /> <BR>
    				Onthoud:&nbsp;&nbsp;<INPUT TYPE="checkbox" NAME="check1" VALUE="Check1">Username&nbsp;&nbsp;
    				&nbsp;&nbsp;en/of&nbsp;&nbsp;&nbsp;<INPUT TYPE="checkbox" NAME="check2" VALUE="Check2">Password&nbsp;&nbsp;&nbsp;&nbsp;<BR>
    				<A Href="">Password vergeten?</A>&nbsp;&nbsp;<INPUT TYPE="button" VALUE="Click" onClick="testButton(this.form, this.form.UsernameField.value, this.form.PasswordField.value)">
    			</FORM>
    		</DIV>
    	</P>
    </DIV>

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles