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: <INPUT Type="Text" Maxlength=11 Name="UsernameField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Username"/>
Pass: <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
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.
WOW, it works, thank you very mutch!!!
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: <INPUT Type="Text" Maxlength=11 Name="UsernameField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Username" />
Pass: <INPUT Type="Password" Maxlength=11 Name="PasswordField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Password" /> <BR>
</Form>
<FORM NAME="this" >
Onthoud: <INPUT TYPE="checkbox" NAME="check1" VALUE="Check1" > Username
en/of <INPUT TYPE="checkbox" NAME="check2" VALUE="Check2" > Password <BR>
<A Href="" > Password vergeten?</A> <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!!!!!
Are you getting a JavaScript error? If so, please post it. Can you also explain a little more about how this is failing?
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!
The <input>s and <button> need to be inside the same <form>
Code:
<DIV Class="transbox">
<P>
<DIV Align="Right">
<Form Name="Login">
Username: <INPUT Type="Text" Maxlength=11 Name="UsernameField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Username"/>
Pass: <INPUT Type="Password" Maxlength=11 Name="PasswordField" Style="WIDTH: 80px; HEIGHT: 20px" Size=11 Id="Password" /> <BR>
Onthoud: <INPUT TYPE="checkbox" NAME="check1" VALUE="Check1">Username
en/of <INPUT TYPE="checkbox" NAME="check2" VALUE="Check2">Password <BR>
<A Href="">Password vergeten?</A> <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
Forum Rules
Bookmarks