jQuery is one really awesome JavaScript library, and there is definitely no argument that it makes life easier. But its robustness can be its downfall sometimes. You can do almost anything with it, but with some things it is hard to figure out just how to do it. Today I will take you through some jQuery selectors that you may have not known about concerning forms.
Forms are everywhere, and more than likely if you have done web development, one of the first things you did was put a form of some kind some place on a web page. But, in today's world of web 2.0, you want something fast and something that provides a lot of feedback. Most of the time, we use JavaScript to add function to these forms, and using jQuery it can be really easy. But, getting the information in a form, using jQuery can be a little tricky sometimes. Lets start with the basics.
If you have used jQuery before, then you are probably aware of the val() function, which grabs the value of the selected tags. So, lets consider the following:
<input type="text" id="myTB" value="A Textbox....."/>
In order to get or modify the contents of either, you use the val() method like so:
var tbText = $('#myTB').val();
//And to modify the text
var taText = $('#myTA').val("Some new text.....");
The use of the val() method is pretty straight forward and easy to understand. But, some form elements are a bit trickier. Lets say you have a nice, plain ol' html select:
<option value='1'>Opt 1</option>
<option value='2'>Opt 2</option>
<option value='3'>Opt 3</option>
</select>
How would you get the selected element? After looking through the documentation, the answer still might elude you. To do this, we are taking advantage of the :selected filter, which allows us to grab the, well selected option. Putting it all together we get:
var selVal = $("#mySel option:selected").val();
//To get the option's text
var selText = $("#mySel option:selected").text();
It's simple enough, but it is not too obvious how to go about doing that. Something slightly more obscure is how to handle checkboxes. Let's say you have just a checkbox, and you want to find out if it is checked or not:
So, in order to find out if this is checked or not, the easiest way to go about doing it would be with a method called is(). All this does is check the selected elements for specific filters. Luckily, there is a :checked filter we can use, so testing the checkbox goes like so:
var isChecked = $("#myCB").is(":checked"));
Then you can use this information however you want. This will give you an answer on whether the checkbox is checked or not. Slightly more complex are radio buttons, which come in groups. Lets take the follow set of radio buttons:
<input type="radio" name="rg" value="r2"/> Radio 2<br/>
<input type="radio" name="rg" value="r3"/> Radio 3
What we want here is to find which one of these is the currently selected ones. To do this, we have to first get all of the radio buttons in this group, so we use the name attribute. Then we use the :selected filter like we did on the select. The whole ordeal will look something like so:
Since there is no real text inside the radio button, we have to use the val() method. As long as we have a way to decode what that value means, we are in business.
These are some form selectors that I had to dig a little for, and they certainly are not that obvious unless you spend hours reading all the jQuery docs. Hopefully these will save you some digging of your own. Just remember, when you need coding help, just Switch On The Code.
09/04/2009 - 09:11
Did you know you can use .val() with SELECT elements too? If multiple options are selected, it will return an array.
09/04/2009 - 11:20
Cool, thanks for the tip.
09/06/2009 - 21:21
YW.
This post is a really nice list, showing the power of jquery. Reading the docs is a bit of a pain, but www.visualjquery.com makes finding useful functions a snap :)
09/10/2009 - 15:56
That is interesting to know, thanks a bunch Nathan.
10/29/2009 - 18:33
nice, but no demos/examples...???
01/16/2010 - 02:37
This is really useless. Try doing it all w/o ids - that would be a hair more useful when talking about selectors.
05/06/2010 - 10:55
I'm really late to this discussion but there is nothing wrong with using ids, especially since it makes it much faster than using other selectors.
05/05/2010 - 13:14
i don't think it was useless at all, it was quite informative - definitely one for the printer, lol.
i assume it can be done without id or classes by using the element name along with the eq() function
like div(0).eq(2).val for example to get the value of the 3 element in the firt div, just psuedo code, i'm not sure how to code it properly.
i know document.images(2).scr would give the source of the 3rd image on the page, so it would probably be similar.
most people do use ids or classes now though even though you can use the dom to find what you want without using ids or classes, but then you kinda need them to apply your css anyways.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.