From the course: JavaScript: Enhancing the DOM (2013)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Working with named form elements

Working with named form elements

From the course: JavaScript: Enhancing the DOM (2013)

Start my 1-month free trial

Working with named form elements

Selecting form elements is a little easier because forms can have and additional attribute called Name. Like with document images, the DOM provides a special object called document.forms. That's an array of form elements on the page. You can also get to any form element that has a name a little bit easier. So, let's take a look. I'm going to go over to my website, and I'll click on this Register page. Now from here, you can see that I have a form on the left side of this document. Let me pull up the developer tool by hitting Cmd+Option+I on a Mac, Ctrl+Alt+I on a PC. And then, I'm going to hit the Escape key to pull up the console, and I'll type in document.forms. Now, that gives me an array of all the forms in the current document. I've only got one form, so it's only returning one item. If I want to get into that element, I can say document forms and then the index. Since this is the first item, I'm going to use the index 0, because arrays are 0 indexed. Now, I can get to the…

Contents