Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Looking up form elements

From: Validating and Processing Forms with JavaScript and PHP

Video: Looking up form elements

Once you know how to gain access to a form field like an input field, checkbox, or a drop-down, we can access any of the attributes within that field via the elements array. In the last movie, we learned that we can get to a form field in one of two ways. Using the ID of the field and get elements by ID, or using the name of the field and the document forms array. Once we're there, we can retrieve any sets of attributes using dot notation. Now it's important to realize that different field types have different attributes. We need to know how to get to the different things in our forms.

Looking up form elements

Once you know how to gain access to a form field like an input field, checkbox, or a drop-down, we can access any of the attributes within that field via the elements array. In the last movie, we learned that we can get to a form field in one of two ways. Using the ID of the field and get elements by ID, or using the name of the field and the document forms array. Once we're there, we can retrieve any sets of attributes using dot notation. Now it's important to realize that different field types have different attributes. We need to know how to get to the different things in our forms.

I'm going to be working on this form, which is a form that I created in my course CSS styling forms. I show you how to put it together on chapter six. So I'm going to rightclick in the background and select Inspect Element. I'm using the Google Chrome browser, and I'm going to hit the Console tab on the right hand side. And once here, I'm going to try to acces the my name field, which is the place where the user types in their name. By typing in document, get elements, my ID and then typing in the ID of that field, which is my name.

I hit Return, I can get to that specific filed. We can also get to the that field by typing Document.form, which is the name of the form, and the name of the field, which is my name. If we hit return, we get to the same field. So notice that this field has a number of attributes. To get to any of those attributes, you can simply types the names of the attributes using dot notation. So if we type Document dot the form dot my name we can get to the type field by just typing in type.

We hit Return, it gives us the value of type. And we can access any of the other fields like that. So if you say name, it'll give us my name, etc, etc. Now you can also modify any of them, by setting them as well. So for example, I can actually change the form type, let's go to the type right here and set it to be a radio button. I need to make sure that I put this in quotes. So, now the name field happens to be a radio button. Let's go ahead and set it back. A common thing to do when validating and processing forms is to set the value of a field.

So, if I want to set a value of a field, all I need to do is type in value and type in whatever I want in there. I'll put my name. Now you can see that the name field gets the value that I just entered. Let's try getting and setting some of the other attributes in our form. This will teach you how to access different things. So what do we do if we want to access a checkbox? So right now this checkbox called Subscribe Item is checked, so we can uncheck it by typing in Document the form, and then it's called scribe item.

And then use the checked attribute, and set it to false. Now, only quotes on this one, and now you see that that item has become unchecked. What about if we wanted to modify a drop-down menu? That's a little bit trickier. To get to the menu itself, we can use document.theform. And that field is called Reference. But if we want to access each of the elements in there, I need to use an array notation. Notice if I open this triangle, I can see that there are a few elements in there. So if I want to get the third element, I can use Array Notation Number Two.

And then add a selected attribute to that field and set it to true. Now, that Option is the selected Option. So, most validation deals with check in and setting the values of existing fields. It's a good idea to pull up this form and get comfortable with accessing and modifying different form fields.

Show transcript

This video is part of

Image for Validating and Processing Forms with JavaScript and PHP
 
Expand all | Collapse all
  1. 3m 36s
    1. Welcome
      1m 9s
    2. What you should know
      1m 2s
    3. Using the exercise files
      1m 25s
  2. 12m 50s
    1. Understanding forms
      2m 2s
    2. Working with form fields
      7m 4s
    3. Using the form tag
      3m 44s
  3. 19m 23s
    1. Using input validation
      2m 10s
    2. Adding required fields and placeholders
      1m 31s
    3. Constraining numeric and date fields
      2m 32s
    4. Accepting multiple entries
      1m 41s
    5. Limiting uploads by MIME types
      2m 35s
    6. Assisting text input with a datalist
      1m 55s
    7. Constraining with regular expression patterns
      6m 59s
  4. 42m 13s
    1. Accessing forms
      3m 57s
    2. Looking up form elements
      3m 35s
    3. Handling focus changes
      2m 47s
    4. Detecting the onchange event
      4m 31s
    5. Using the selectedIndex property
      2m 30s
    6. Dynamic validation with regular expressions
      7m 0s
    7. Creating a generic input validation function
      4m 31s
    8. Validating in older browsers with Modernizr
      7m 32s
    9. Interrupting form submission with onsubmit
      5m 50s
  5. 15m 20s
    1. Understanding jQuery
      3m 47s
    2. Validating on submit with jQuery
      3m 45s
    3. Building interactive jQuery validation
      2m 34s
    4. Using the jQuery Validation plugin
      5m 14s
  6. 32m 57s
    1. Communicating with PHP servers
      2m 27s
    2. Retrieving data from superglobals
      8m 18s
    3. Using server-side validation
      4m 59s
    4. Adding in-page validation
      5m 22s
    5. Mirroring input data back to the user
      7m 46s
    6. Sanitizing form input
      4m 5s
  7. 43m 29s
    1. Mailing form data
      8m 28s
    2. Understanding file uploads
      3m 1s
    3. Uploading files
      9m 20s
    4. Processing form data with AJAX
      8m 14s
    5. Preparing your database
      5m 50s
    6. Pushing data
      8m 36s
  8. 1m 17s
    1. Next steps
      1m 17s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Validating and Processing Forms with JavaScript and PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.