jQuery: Handle Dropdown (select), Checkboxes and Radio selections
Posted by Michael Gaigg
Almost daily I work with lists, be it dropdown, checkbox or radio selections. I need to select values, submit selections, hide/show containers depending on the selection etc.
I tend to forget all these cool jQuery notations that make every programmers life easier. How nice would it be to have a little cheat sheet? So I decided to summarize my findings (and share them with you).
Obviously jQuery is powerful and gives you a multitude of possibilities so this list is neither exhaustive nor covers all possible application scenarios. Mix, match, combine and extend the samples as you wish and feel free to post your additional solutions in the comments section.
See the test page.
Dropdown
I'm using the following sample list:
<select id="ddlCategories"> <option>- Select a category -</option> <option value="books" selected="selected">Books</option> <option value="videos">Videos</option> <option value="dvds">DVDs</option> </select>
Get value of selected item
Pure and simple, despite what you might read in other tutorials:
alert("Selected value: " + $("#ddlCategories").val());
Change text of a particular item
Let's target item with value 'videos':
$("#ddlCategories option[value='videos']").text("Videos and tapes");
Change color of a particular item
This could be useful to show that the category is currently unavailable or a certain item is out of stock.
$("#ddlCategories option[value='videos']").css("color", "#CCC");
Get number of options
Don't forget to subtract options that don't count, e.g. the empty "- Select a category -" option.
alert("Number of options: " + ($("#ddlCategories option").length - 1));
Set a particular option
Set the option 'videos' to be the selected item.
$("#ddlCategories option[value='videos']").attr("selected", "selected");
Checkboxes
Our sample checkbox list:
<div id="chkCategoriesContainer"> <input type="checkbox" name="chkCategories" value="none" checked="checked" />None<br/> <input type="checkbox" name="chkCategories" value="books" />Books<br/> <input type="checkbox" name="chkCategories" value="videos" />Videos<br/> <input type="checkbox" name="chkCategories" value="dvds" />DVDs<br/> </div>
Check all items in the checkbox list
This could be invoked e.g. through a link named 'Check all'.
$("input[name='chkCategories']").attr("checked","checked");
Uncheck all items in the checkbox list
Same here, uncheck all, a very common requirement.
$("input[name='chkCategories']").removeAttr("checked");
Make the second item being checked
List is zero-based, so the second item is nth(1)
$("input[name='chkCategories']:nth(1)").attr("checked","checked");
Show the value of each checked item (requires a container!)
In this case we simply alert the value, but we could might as well write it to another output field, container or whatever you may wish.
$('#chkCategoriesContainer :checkbox:checked').each(function() { alert($(this).val()); //$('#outputField').append(', '+$(this).val()); });
Radio
Our sample list again:
<div id="rdoCategoriesContainer"> <input type="radio" name="rdoCategories" value="none" checked="checked" />None<br/> <input type="radio" name="rdoCategories" value="books" />Books<br/> <input type="radio" name="rdoCategories" value="videos" />Videos<br/> <input type="radio" name="rdoCategories" value="dvds" />DVDs<br/> </div>
Get value of checked item
alert("Selected value: " + $("input[name='rdoCategories']:checked").val());
Append item to container
The item will be added to the DOM and is available to jQuery immediately.
$('#rdoCategoriesContainer').append('<input type="radio" name="rdoCategories" value="tapes" />Audio tapes<br/>');
How to Disable Button on form submit in .NET
Posted by Michael Gaigg
This problem seems to be almost too obvious to be posted here but it took me quite some time to actually figure it out how to do it correctly - so I'd might just share it with you.
Disabling the submit button helps users comprehend that their action is in process and waiting for a response can be expected. It also prevents them from clicking the same action more than once which could lead to serious troubles (duplicate entries, application exceptions, etc.)
Problem
How do I disable a form submit button on a .NET page that does client-side validation?
The problem is that the button cannot simply be disabled because it would not be enabled again if the client-side validation prevents the form from being submitted.
Solution
.NET
<asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClientClick="SubmitForm(this);" />
JavaScript:
function SubmitForm(source) {
ret = true;
if (typeof (Page_ClientValidate) == 'function') {
Page_ClientValidate();
ret = Page_IsValid;
}
if (ret) {
source.value = "Processing...";
source.disabled = true;
__doPostBack(source.name, "");
}
return ret;
}
Recent Posts
- 500+ Free Stunning PC Icons August 31, 2010
- Highlights of Week 32/2010 August 16, 2010
- How to use the Google Font API August 10, 2010
- Secret of Pixar’s Success [Link] August 6, 2010
- Esri listens to their Users and changes Pronunciation of its Name August 3, 2010
- Highlights of Week 30/2010 August 2, 2010
Categories
Accessibility (13)
Code Samples & Tips (4)
Design Guidelines (11)
Go figure (7)
Good News (11)
Here and there (11)
This Week’s Highlights (23)
Usability & UCD (14)
Web Design (13)
Popular Posts
- Make it easy for your customers to throw money at you!
- Web Content Accessiblity Guidelines (WCAG) 2.0: Overview and Structure
- Section 508: WCAG 1.0 or WCAG 2.0?
- Powerpoint Wireframe Stencils as Free Download
- Go figure: 10 Comic Strips that have Something in Common
People that inspire me
- Björn Seibert
- Boxes and Arrows
- Bruce Temkin
- Christian Heilmann
- David Leggett
- Gez Lemon
- Hannes Schmiderer
- Harry Brignull
- Henny Swan
- Jared Smith
- Jared Spool
- John Rhodes
- Joshua Porter
- Kel Smith
- Scott Berkun
- The Access Pond
Tags
What I'm Doing...
- Top 20 Wireframe Tools http://t.co/zRBd0W0 via @garmahis 1 week ago
- RT @wpzoom 500 Free Icons: WPZOOM Social Networking Icon Set http://bit.ly/aEGdUJ 2 weeks ago
- RT @esriuc GIS: Designing Our Future. The concept of GeoDesign is really taking off, keep an eye out. http://bit.ly/9TioWQ 3 weeks ago
- 1400 super cool web icons by FatCow: http://bit.ly/ctskcU 2010-08-11
- [JOB OPENING] User Interface Engineer, http://bit.ly/ui-engineer-position 2010-07-30
- More updates...
Posting tweet...