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
From the course: Validating and Processing Forms with JavaScript and PHP
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Accessing forms3m 57s
-
Looking up form elements3m 35s
-
Handling focus changes2m 47s
-
Detecting the onchange event4m 31s
-
Using the selectedIndex property2m 30s
-
Dynamic validation with regular expressions7m
-
Creating a generic input validation function4m 31s
-
Validating in older browsers with Modernizr7m 32s
-
Interrupting form submission with onsubmit5m 50s
-
-
-
-
-