Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Accessing forms is similar to accessing other HTML elements but because of their nature there are some additional methods and properties we can take advantage of. If you'e given your form an ID, the easiest way to have access to the form element is by using getElementById, but with forms, you can also look up elements by name. So here's a form I've created with a number of different types of fields. If you're interested in how this form was put together, make sure you check out my course on CSS styling forms. I put together this form on chapter six. So I'm using the Google Chrome browser and I need to look at the Developer tools.
Since I'm using Google Chrome, I can hit the up arrow to get to the last command. So, I'll just modify this to my name, and hit return, and now I get just the input field with the user's name. This is one of the reasons why it's a good idea to add IDs to your form elements. Of course, you can also use the getElementByTagName method to target all the input fields in our form. So, let's try that. And I'm going to add for all the input tag names. Now we get a list of all the input fields. And if you'll notice a little bracket's on each end of our list, you can tell that this is an array form. So, if I hit the up arrow, and I attach some brackets, and then type the index of the element that I want, I'll ask for the first element. Then I can get just to that field which is the same as the field we had before. This is the name that we're asking the user for. Now this will list all the forms in the current page. This page has only one form so there's only one element. This is probably a dangerous way to access a form since the number of forms on a page can vary from time to time.
It's probably better to refer to it by its ID. This form has an ID of My Form, so I can just type this, and I get the form again. So although the preferred way to access elements with JavasScript is usually by working with IDs, forms also have a special attribute called Name. Using the document forms array, you can access an element by name instead of by index number. We can even shorten this further by adding a name to the form itself. This form has a name of The Form.
Now to get to the name field we can simply type, document.theform.myname. If we try to access an element with multiple values like a radio button you can get an array of elements. Here's a couple of radio buttons. The name of those buttons are, request type. So here we get an array of two input fields, and if we want to access one of them, we can use an array notation. Here's the first one, and here of course is the second one. If we try to access an element like a select field we'll get just the select field itself.
So we have select field called reference. Now we don't get the individual options inside reference but we can target those with an array like notation. So to get the first option, I will type in array0. And that gives me the choose option which is the default. You can see some of the other options here. So I can get to those by typing 1, 2 or 3. To get to each of the options use an array notation similar to what we did with radio buttons. Most validation deals with accessing specific form elements usually by using an ID, however sometimes if you use names in your forms you can get to them quicker by using the document forms array.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.