jQuery Tip : How to Select Radio/Checkbox Input On Click

Saturday, January 10th, 2009

I've been faced with the following scenario a few times during my web development career : First, you have an unordered list - within each list item you have a radio input. Secondly, when you click on the list item (i.e. the entire container), it must select the radio input and add a css classname of "selected" to the list item, thus highlighting the item that has been selected.
The following example will explain how to implement a workable solution. For a preview of what we will achieve, check out the demo.

The HTML :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<!-- include our jquery scripts -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
    <script src="js/radio.js" type="text/javascript"></script>
    <style type="text/css">
    	li { color: #000; }
    	li.selected { color: #FF0000; }
    </style>
</head>
<body>
	<div>
		<ul>
			<li class="test">Test 1 <input type="radio" name="test" /></li>
			<li class="test">Test 2 <input type="radio" name="test" /></li>
			<li class="test">Test 3 <input type="radio" name="test" /></li>
			<li class="test">Test 4 <input type="radio" name="test" /></li>
			<li class="test">Test 5 <input type="radio" name="test" /></li>
		</ul>
	</div>
</body>
</html>

There is nothing out of the ordinary here - just a simple unordered list of radio inputs.
There's a basic stylesheet to highlight "selected" items.

For those of you wondering why I'm including the jQuery library from the Google Code repository :

  1. It saves on bandwidth, it'll load very quickly from Google's CDN.
  2. It'll already be cached if the user has visited a site which also delivers it from Google Code.

Our Javascript/jQuery:

$(document).ready(function(){
	$('li').click( function(){
		$('li.selected').removeClass('selected');
		$(this).addClass('selected').children("input[@type=radio]").click();
	});
});

As you can see, the actual code involved is short and straightforward - just the way we like it.

  1. First - we tell jQuery to execute the following code once the document has finished loading, ensuring that the DOM has been registered successfully.
  2. Next we bind a click event to all list item elements on the page - you can of course change this to all list items with a class of "myselection" for example ( $("li.myselection").click... )
  3. When a list item is clicked - we first need to remove the "selected" class from any previously "selected" list items.
  4. Now we need to add the "selected" class to the list item which fired the event ( $(this) ).
  5. Finally, we need to make sure that the radio button inside the list item is "clicked" : we do this by using the .children() selector and executing a click() event on the input.

The important bit is the .children("input[@type=radio]") selector. Known as an XPath selector. What we're doing here is telling jQuery to find all all radio inputs inside "this" list item. We then use .click() to..as you guessed it... execute a click event on the returned element (in this case the radio input).

And that's all there is to it. You can read up some more on jQuery selectors here.
I would also highly recommend reading the following article : Improve your jQuery - 25 excellent tips.


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Barry is a self professed PHP ninja and jQuery guru wannabe. Currently working as a freelancer, his free time is divided between personal projects and trying to help other developers find useful resources and solve common problems. You can learn more about Barry and his work on his website, or you can catch up with him via twitter ( @barryroodt ).
  1. January 10, 2009 at 7:47 pm
  2. January 10, 2009 at 11:31 pm
  3. Sonda
    January 21, 2009 at 3:12 am
  4. January 27, 2009 at 7:26 am
  5. January 27, 2009 at 7:26 am
  6. S.A.K
    March 10, 2009 at 11:52 pm
  7. May 11, 2009 at 9:27 am
  8. Kiran Kumar
    July 28, 2009 at 9:27 am
  9. Yue
    November 26, 2009 at 5:04 pm
    • November 26, 2009 at 9:41 pm
      • Paul
        December 19, 2009 at 3:35 pm
      • December 20, 2009 at 12:25 pm
  10. December 16, 2009 at 1:11 am
  11. Adam
    March 9, 2010 at 4:48 pm

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates