User:Pmw57/form-handling-techniques

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Forms can be accessed either via the forms collection, or a unique identifier. Using a unique id for the form is an effective technique that allows good flexibility as to how the form is accessed and worked with from the scripting.

<form id="login">
    <p><label>Username <input name="username"></p>
    <p><label>Password <input name="password"></p>
</form>

You can attach a scripting event to the form with:

var form = document.getElementById('login');
form.onclick = function () {
    ...
};