Access form elements JavaScript


show more Learn how to access HTML form elements with JavaScript using document.forms.name and getElementById. Simon Allardice explains the different kinds of form elements, such as text fields, check boxes, radio buttons, and dropdown lists, and the types of events they generate, including the crucial Submit event. show less
please wait ...

Accessing form elements

When it comes to working with forms, we care about several things: first, the values of the individual form elements, and then the events caused by those form elements when you change them or when you move in and out of them, and of course the event of the entire form itself, in particular submitting the form. Now, if you've given your form and the fields inside it their own IDs, then you can of course use document.getElementByID to retrieve either the form or any field in the form.

And if the HTML for your form has either an ID or a name property-- a name property is quite common with forms-- you can use the following format too: document.forms.name of the form. In this case, it was frmContact. And if you use the name property in your HTML for your individual form elements, you can use that directly too, document.forms.name of the form .name of the element. But I'm still a fan of the good old document.getElementById.

So let's talk about the individual form elements first. Pro...

Accessing form elements
Video duration: 4m 20s 5h 31m Beginner

Viewers:

Learn how to access HTML form elements with JavaScript using document.forms.name and getElementById. Simon Allardice explains the different kinds of form elements, such as text fields, check boxes, radio buttons, and dropdown lists, and the types of events they generate, including the crucial Submit event.

Subjects:
Developer Web
Software:
JavaScript
Author:
please wait ...