From the course: Validating and Processing Forms with JavaScript and PHP

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Using the selectedIndex property

Using the selectedIndex property

Just about all the text fields in an HTML form are input fields. The exceptions are the text area field, which is really just like a text field, and also the select field. The select field is quite another beast, so I wanted to cover how to handle onchange events on select fields. Select fields have a special and very useful property called the selectedIndex property, which shows you the option that a user has selected from its list. So, let's take a look at this form, which we're going to be working on in this movie. This form just has a single field, and it's a pop up field with a list of some websites. If you take a look at the code, you'll see that the value field for each of those is an actual site. So what I'm going to do is, after the form field, I'm going to add a script tag. And on that script tag, I'm going to target the form. And our field is called reference, so I'll target that. And I'll add an onchange event handler. That's going to be a function literal, means just a…

Contents