Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q788 How can I write a generic function that will accept parameters from a form button and update one of many different textarea with the contents of one of many different select form fields in one of many different forms?

You are here: irt.org | FAQ | JavaScript | Form | 10.2 | Q788 [ previous next ]

The following should be generic enough for you:

<HTML>
<SCRIPT LANGUAGE="JavaScript"><!--

function additive(f,s,t,n) {
    document.forms['f' + f].elements['t' + t + 'n' + n].value += document.forms['f' + f].elements['s' + s].options[document.forms['f' + f].elements['s' + s].selectedIndex].text + "\n";
}
//--></SCRIPT>

<BODY>

<FORM NAME="f1">
<SELECT NAME="s1">
<OPTION>AAA
<OPTION>BBB
<OPTION>CCC
</SELECT>

<BR><INPUT TYPE="button" VALUE="->>" ONCLICK="additive(1,1,1,1)">
<BR><INPUT TYPE="button" VALUE="->>" ONCLICK="additive(1,1,1,2)">

<BR><TEXTAREA NAME="t1n1" COLS="30" ROWS="10"></TEXTAREA>
<BR><TEXTAREA NAME="t1n2" COLS="30" ROWS="10"></TEXTAREA>

<HR>

<SELECT NAME="s2">
<OPTION>DDD
<OPTION>EEE
<OPTION>FFF
</SELECT>

<BR><INPUT TYPE="button" VALUE="->>" ONCLICK="additive(1,2,2,1)">
<BR><INPUT TYPE="button" VALUE="->>" ONCLICK="additive(1,2,2,2)">

<BR><TEXTAREA NAME="t2n1" COLS="30" ROWS="10"></TEXTAREA>
<BR><TEXTAREA NAME="t2n2" COLS="30" ROWS="10"></TEXTAREA>

</FORM>
</BODY>
</HTML>

The additive() function expects to be passed the form number (f), the select field number(s) and the textarea number (t and n).

These are then expanded to make up the full object name of the elements to affect, where the forms are named f1, f2... fN, the select fields are named s1, s2... sN, and the textareas are named t1n1, t1n2, and t2n1, t2n2, and can be extended to tNnN.

Feedback on 'Q788 How can I write a generic function that will accept parameters from a form button and update one of many different textarea with the contents of one of many different select form fields in one of many different forms?'

©2018 Martin Webb