1. Computing & Technology

JavaScript By Example

DOM Form: 2. Access The Form Itself

From , former About.com Guide

One of the few fields in a form that does not need to have an id associated with it is the form tag itself. There is no need to go adding one just so that we can reference the form tag from within our JavaScript though. We can access the form tag using any field within the form that has an id because all of the form fields in a form have a form property that refers to the form itself and so we can start from any of those in accessing the entire form.

In this example we use the email input field from within the form to gain access to the form tag and update the action that will be performed when the form is submitted.

HTML


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Example F02</title>
</head>
<body>
<form action="#">
<div>
<label for="email">Email: l</label>
<input type="input" >id="email"> name="email" value="" /><br>
<input type="submit" value="Submit" />
</div>
</form>
<script type="text/javascript" src="exampleF02.js"></script>
</body>
</html>

JavaScript


document.getElementById('email).form.action = 'display.php';

©2012 About.com. All rights reserved.

A part of The New York Times Company.