August 11, 2000 - Cross-Element Referencing via a Back Door | WebReference

August 11, 2000 - Cross-Element Referencing via a Back Door

Yehuda Shiran August 11, 2000
Cross-Element Referencing via a Back Door
Tips: August 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Back-door referencing allows you to access a form from its descendent elements. The form property enables this back-door access. When you send the object this to an event handling function, the ancestor form object cannot be restored, unless you use the back-door form property of the element. The following example demonstrates this concept. When you click the button, the button's object (this) is sent to the event handling function. This function accesses the second element of the form (text field) through the back-door form property, and prints out the text entry currently displayed in the entry field. Here are the button and the text entry field:

Here is the script and HTML segment that implement the above demo:

<SCRIPT LANGUAGE="JavaScript">
<!--
function getValue(otherElement) {
  alert(otherElement.form.elements[1].value);
}
// -->
</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="click me" onClick="getValue(this)">
<INPUT TYPE="text" VALUE="Have you checked our latest tools?" SIZE=30>
</FORM>