Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In addition to providing selectors that work across generic elements of a document, jQuery also has a set of selectors and filters that are just used for dealing with form elements. They work pretty much like any other selector, but they happen to start with a colon character, like filters do. These kinds of selectors and filters make it really easy to build form processing logic using jQuery. So you can see the table here. This table lists the available selectors for form elements, so they pretty much match up directly with the names of the various form elements that you will find in a document.
So for example, you have selectors for input, and text, and password, and radio, and checkbox, submit, all the way down. You can use these to select those individual kinds of elements on the form. So in addition to selectors, there are form filters, and these are the form filters that are available. So there is only a few of these, and they are essentially convenience filters to help you select form elements that are in certain state. So for example, we have a filter for enabled and disabled, and we have got a filter for check and selected.
So for example, the checked one matches form elements that are checked, and obviously it applies only to radio buttons and check boxes, whereas the enabled and disabled will match form elements that match that state. So let's go ahead and fire up our code editor and take a look at a couple of form examples. So I am going to jump over to the code here. I am going to go ahead and open up the Formselectors_start file, and that's in the Chapter 2 section of the exercise files.
Now if you've got access to the exercise files, you can open this up in your own text editor or just follow along with me watching the movie, or you can go ahead and skip ahead to the _finished version to see how everything works. So I'm going to go ahead and open this in my editor. So this example is a little bit different than the other examples we've looked at so far, so let's begin by looking at the Design view. And you can see what we have is a source file with several form fields in it. We've got some input fields, we've got some radio buttons, check boxes, and some other things.
So let's go back to the source and take a look at how we use jQuery to select each of the form elements. Okay, so up here in my document.ready function I'm going to start by writing my query. And I'm going to do things a little bit differently than I did in my previous examples. In my previous examples, I was surrounding elements with a red border box, but not every browser supports doing that with form elements, so I'm going to do something a little bit different. Let's begin by selecting form elements that are input fields.
So I'm going to type form: input. And then to show you, visually, that the selector is working, I'm going to type "before" and this is going to put some text before the selected element. So instead of doing the red border, I'm going to do this. Okay. So what this is going to do is select all the input elements of the form and then put the word "INPUT" in front of the form element. So let's save. Let's go over to the browser and refresh. Okay, so I'm going to refresh this in the form.
Okay, so you can see that when I refresh the document, the word "INPUT" got put in front of every form element that is an input element. And since all of these elements are input elements, you can see that the radio boxes and the check boxes and the text fields and everything had the word "INPUT" put in front of it. Okay, so let's get a little bit more specialized. Let's go back to the code. So, back here in the code, let's try something else. Let's comment this out, and then we'll copy and paste. What we're going to do this time is select text elements.
So in this case, we're going to change this to "text" and we're going to put the word "TEXT" in front of all the text elements. So I'm going to save, and we're going to go back to the browser. And now when I refresh you can see that only the text elements have been selected by that jQuery selector. So none of the other elements now have text in front of them--only these two guys do. And let's go over to the Firefox browser, so I can show you it working there. Okay, here is the same document. And I refresh, and you can see that the word "TEXT" got inserted based on the query we wrote in front of the text elements.
Now let's go back to the source. What I'm going to do now is comment this guy out. And I'm going to switch over to Design view, and I'm going to insert a row in front of this guy. Now you can do using whatever HTML or text editor you're using. What I'm doing is inserting a table row, and I'm going to put in a Disabled element. And I'm going to put in a text field, using my toolbox. Again, you can use whatever favorite HTML that you're using.
Okay, so now I'm going to make this guy disabled. So we'll go to the source and we'll type "disabled=disabled." Okay, so he's disabled now, and what we're going to do is write another query that shows how to select elements that are enabled. So let's copy and paste there. We'll uncomment. So what we're going to do now is just do form and text, but we're going to put the enabled filter on it.
And so I'm going to change this to read "ENABLED." I'm going to save. So now, what this will do is put the word "ENABLED" in front of text fields, but only the enabled ones. So let's switch back to the browser. Okay, and I'm going to refresh. And you can see that even though I've got three text fields, only the enabled ones have the word "ENABLED" in front of them; this disabled guy does not have text in front of it. Okay, so let's take a look at Firefox. Okay, here's the Firefox version.
And I refresh and you can see that it's working there as well. Let's go back to the source. So if I were to change this to disabled then the reverse would happen; only the disabled field would have the text in front of it. Okay, so let's comment this. Okay, so for the next example what we're going to do is show how to use jQuery to select items that are checked. So let's go back to Design view. And we're going to have some items that are checked by default. So what we're going to do is check this radio button and this check box and this check box.
So let's go into the source and do that. I'm going to--let's say this guy equals checked. And we'll check this first checkbox as well, and we'll copy that and put it on this one as well. So let's go back to the Design view. So you can see now that several of the elements are now checked: this radio button and these check boxes. Let's go back to the jQuery code now, and let's go to the top.
What we're going to do now is write a selector that selects form elements that are checked. And I'm going to use just the filter by itself here, and I'm going to write "before" and I'm going to insert some text that says "CHECKED." Okay. So let's save. Let's go back to the browser and see what happens. Okay, here were are, in the browser. I'm going to refresh. And you can see that only elements that are checked, including the radio button and these check boxes, have the word "CHECKED" put in front of them, whereas these unchecked items, these two check boxes and this radio button, do not.
So that's pretty useful, and you can see how jQuery can be pretty powerful for processing form elements, selecting things that are checked and enabled, and so on. Let's do one more example. What we're going to do now is only select check boxes that are checked, so we're going to write a short query that ignores the radio button. Okay, let's go back to the source. Okay, so here we are, in the source, and I'm going to comment this guy out and do a copy and a paste. And I'm going to save.
So now I'm going to writing in front of the checked filter just ":checkbox". What this is going to do now is look inside our form for all the check boxes that have "checked." And I'm going to save. Okay, let's go out to the browser again. So now when I refresh, you can see that the word "checked" does not appear in front of the radio button, only the check boxes. And I could the code to go back and, instead of writing checkbox, I could write radio button. Let's go back over to Firefox and see it there.
Okay, so I'm going to refresh, and you can see it's working here as well. Okay, so that's how you use jQuery to process form elements. You can see that this is a very powerful feature. It makes form processing a lot easier. Let's go ahead and move on.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92425 Viewers
82 Video lessons · 104131 Viewers
71 Video lessons · 75569 Viewers
56 Video lessons · 106949 Viewers
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.
Your file was successfully uploaded.