I am wanting many different buttons (each written in using PHP), to pass 3 or more variables using onClick to the function to then write this into the input fields.
The following is getting close, but I am not sure how to pass up the multiple var array up to the function for the re-write..
<HTML>
<HEAD>
<TITLE>Test Input </TITLE>
<SCRIPT LANGUAGE="JavaScript">
function writeText (var1, var2, var3) {
form.inputbox1.value = var1
form.inputbox2.value = var2
form.inputbox3.value = var3
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET">
Enter something in the box: <BR>
<INPUT TYPE="button" NAME="button2" Value="Write" onClick="writeText(this.form)"><br>
<input type="text" name="inputbox2"><br>
<input type="text" name="inputbox3"><br>
<input type="text" name="inputbox4">
</FORM>
</BODY>
</HTML>
I am wanting many different buttons (each written in using PHP), to pass 3 or more variables using onClick to the function to then write this into the input fields.
The following is getting close, but I am not sure how to pass up the multiple var array up to the function for the re-write..
<HTML>
<HEAD>
<TITLE>Test Input </TITLE>
<SCRIPT LANGUAGE="JavaScript">
function writeText (var1, var2, var3) {
form.inputbox1.value = var1
form.inputbox2.value = var2
form.inputbox3.value = var3
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET">
Enter something in the box: <BR>
<INPUT TYPE="button" NAME="button2" Value="Write" onClick="writeText(this.form)"><br>
<input type="text" name="inputbox2"><br>
<input type="text" name="inputbox3"><br>
<input type="text" name="inputbox4">
</FORM>
</BODY>
</HTML>
Share
Improve this question
asked Jul 16, 2012 at 23:11
JaybestJaybest
311 gold badge1 silver badge3 bronze badges
3
- I am not really getting your requirement here. Do you mean you want to use the writeText to be called from different buttons where some might pass just 1 value in place of 3? Can you please elaborate? – Wiz Commented Jul 16, 2012 at 23:15
-
Just find the
<input>
elements in the DOM. No need to pass them as parameters. – Pointy Commented Jul 16, 2012 at 23:27 - what do u need to pass, where's this.form defined? – Moataz Elmasry Commented Jul 16, 2012 at 23:30
2 Answers
Reset to default 11If what you are trying to do is have different buttons, each of which writes a different set of values to the three fields, then there are a couple of issues.
- In the writeText function, you are not specifying which form should be written to (unless the 'form' variable contains the form, that is).
- The parameter you are passing to the writeText function is not actually one you have written the writeText function to use.
First, to write three values to the fields using the writeText function conceptually like you've written it, your button HTML should be along the lines of:
<INPUT TYPE="button" NAME="button2" Value="Write" onClick="writeText('value1','value2','value3')">
Also, you need to change the code in your 'writeText' function to point to the fields you want. One way of doing this would be to add an ID to the fields you want to write to, and then getElementById, e.g. change your fields to
<input type="text" name="inputbox1" id="inputbox1">
then in the javascript using:
var inputbox1 = document.getElementById('inputbox1'); if (inputbox1) { inputbox1.value = var1 }
So specifically, to pass the three values from the onClick to the writeText function, you need to use
onClick="writeText('value to write 1','value to write 2', 'value to write 3');"rather than
writeText(this.form)
And how to handle -
$('#list_tasks').append('<button class="btn btn-default" onclick="buildMemberPerTask(' + res + ',' + res + ')"; type="submit">' + res.results[i].description + '</button>');