JAVASCRIPTINGTaking advantage of the elements array

Taking advantage of the elements array

Steven W. Disbrow

Listing 1. Accessing poorly-named HTML controls from JavaScript.

<TITLE>Using the "elements" Array</TITLE>
<script language="javascript">
function doVote( theForm) {
	var pFirst = theForm.elements["first name"].value;
	var pLast = theForm.elements["last name"].value;
	alert( "You voted for " + pFirst + " " + pLast);
<form name="form1">
Enter the name of your favorite American President:<br>
First: <input type="text" name="first name" value="George">
Last: <input type="text" name="last name" value="Washington">
<input type="button" value="Vote"
 onClick="doVote( this.form)">

About the Author

Steven Disbrow is the owner of EGO Systems, a computer consulting firm in Chattanooga, TN. He can be contacted at [email protected].