Joe Lanman

Web design and development when not playing computer games

Usable multiple-selection with checkbox lists and jQuery

Published on March 21, 2009

There are two out-of-the-box methods for multiple selection in HTML: checkboxes and the <select> tag. Neither are perfect – I’ll be discussing why, and proposing a sort of mashup of the best aspects of both, using jQuery.

The vanilla HTML implementations of these two methods are shown below:

1) Select tag:

(Hold ctrl or command to select multiple values)

2) Checkboxes and Label tag:

Note that the <label> tag allows users to click the label to toggle the checkbox on and off.

In terms of usability,  the checkbox method is more straightforward – it is obvious how to select multiple values, whereas with the select method we have to tell the user which keys to hold down. In addition to that, if a user has selected multiple items, and then forgets to hold the correct key on the next click, their selection is lost and only the last value clicked is selected. On a list that can be scrolled, it may not even be apparent that the other selections have been lost.

The disadvantages of the checkbox method are:

  • It’s not obvious that the label can be clicked.
  • It would be nice if a larger area were clickable, as with the SELECT example
  • It would be nice if the entire row were highlighted, as with the SELECT example
  • The values are not scrollable – adding more will simply show a longer list

We’re going to fix all that with some simple CSS and Javascript (using JQuery).

Click to see an example page

Edit 1 – 9/4/09: Simplify code as per Victor’s comments, and fix refresh issue in Firefox

Edit 2 – 9/4/09: Simplified again, using just the label tags, without <a> tags, from the reddit comments


	$(document).ready(function() {

		// make sure labels are drawn in the correct state

		$('label').each(function()
		{

			if ($(this).find(':checkbox').attr('checked'))
				$(this).addClass('selected');

		});

		// toggle label css when checkbox is clicked

		$(':checkbox').click(function(e)
		{

			var checked = $(this).attr('checked');
			$(this).closest('label').toggleClass('selected', checked);

		});

	});

Filed under: Front-end, User Experience
Tags: , , , , ,

7 Comments

  1. Viktor says:

    Hi!

    if (checked)
    {
    $link.addClass(‘selected’);
    $checkbox.attr(‘checked’, true);
    } else {
    $link.removeClass(‘selected’);
    $checkbox.attr(‘checked’, false);
    }

    ==

    $link.toggleClass(‘selected’);
    $checkbox.attr(‘checked’, !($checkbox.attr(‘checked’)));

    ?

  2. admin says:

    Hi Victor, thanks for the comment.

    The problem with the code you posted is two-fold: it assumes the link already has the correct class for its state, and it flips the checkbox state based on the current state.

    The problem with the first is that a lot of browsers try to maintain the state of the form on refresh. This means that if you tick some boxes and then refresh, the selected items would be ticked, but not have the correct class. With your code, if you then clicked, it would toggle to the wrong class again. The solution to this would be to make sure the items have the correct class when the page loads – I’ll look into adding that.

    The problem with the second is that we deal with two different events – clicking the link and clicking the checkbox itself. When you click the checkbox, it changes state automatically before this code gets run. Therefore if you simply toggle the state, it will toggle back to its original state. If you try using preventDefault(), the checkbox will still change state before the code is run, the code will toggle it off again, and then the preventDefault() runs, also turning it off (preventing the default behaviour) so it doesn’t help. This is why I treat the link and the checkbox event separately and pass the desired state to applyChecked().

    If we implement the class checking on page load as I mentioned earlier, then the following code should work:

    $link.toggleClass(’selected’);
    $checkbox.attr(’checked’, checked);

  3. Viktor says:

    Thanks for the reply!
    I’m beginner in Web and JavaScript programming and tips :)

  4. Jason Grant says:

    I have been looking for something along these lines for decades now! Thanks!

  5. eno bassey says:

    hi thanks for this it actually helped me out but wen i view in ie7 or ie6 it doesnt work why?

  6. remi says:

    Hi there. Interesting UI. I join Eno: any idea for a fix for IE6/7 ?

  7. Remi Roques says:

    For IE6/7, try that CSS: got rid of the 26px, padding and the posititon attributes.

    body {
    padding: 40px;
    font-family: Arial, Helvetica, sans-serif;
    }

    h1 {
    font-size: 1em;
    }

    ul#albums {
    width: 220px;
    height: 160px;
    overflow-y: auto;
    overflow-x: hidden;
    list-style: none;
    padding: 0;
    border: 1px solid #CCC;
    }

    #albums li label {
    display: block;
    border-bottom: 1px solid #DDD;
    padding: 4px 4px 4px 4px;
    color: #000;
    outline: none;
    }

    #albums li label:hover {
    color: #000;
    background-color: #EEE;
    }

    #albums li label.selected {
    color: #FFF;
    background-color: #CCC;
    }

    #albums li label .checkbox {
    top: 2px;
    left: 2px;
    }

Leave a Reply

buy Endep where buy cheap Amitriptyline line buy Amitriptyline no visa without rx buy no online rx Donepezil buy Amitriptyline discount Amitriptyline online no prescription Amitriptyline buy on line buy Donepezil and Endep uk Endep Endep no prescription Endep capsule buy Endep pills where can i buy Endep without prescription buy Endep where purchase Endep buy Endep no prescriptions purchase generic Endep online buy Endep fed ex where buy Endep buy Anafranil pay cod Aricept drug cheap Aricept usa generic Aricept cost buy cheap generic Aricept online Aricept no prescription overnight buy cheap Nizoral on line Nizoral without prescription buy Nizoral diet pill buy cheap Nizoral online free consult Nizoral non prescription for next day delivery buy Nizoral online no prescription cheap Nizoral online buy cheap generic Nizoral buy Nizoral mastercard buy discount Lisinopril online order no online rx Lisinopril buy cheap generic Arimidex Arimidex canadian pharmacy order no online rx Arimidex Arimidex buy Abilify order online buy Abilify usa buy Abilify on line without a rx buy Finpecia without prescription to ship overnight Finpecia xr buy online cheap best buy Amitriptyline Amitriptyline from india is it safe next day fedex shipping for Famvir Buy Famvir without a Discount Famvir order Famvir purchase online Famvir without prescription purchase Famvir over the counter cod overnight Famvir fedex shipping buy Amitriptyline legally no rx cod Fluoxetine purchase Fluoxetine online buy Fluoxetine 20 mg buy Fluoxetine no perscription cod Fluoxetine cheap mexican generic Fluoxetine online buy cheap Fluoxetine without prescription Fluoxetine capsule buy no online rx Prozac order no online rx Fluoxetine where to purchase generic Famvir online without a rx Lisinopril bestellen purchase Famvir no visa online without prescription buy in Anafranil uk Anafranil capsule buy cod Anafranil where to buy Anafranil by cod buy Anafranil without a perscription buy Anafranil with american express buy Anafranil cheap buy Nizoral online no rx Lisinopril buy Lisinopril overnight us delivery no prescription Lisinopril purchase online prescription Lisinopril without buying Lisinopril online without rx maxalt online prescription no prescription Maxalt with fedex buy no perscription Maxalt buy Maxalt online pills purchase Maxalt overnight Maxalt online buy saturday delivery buy Maxalt online no prescription Lisinopril online purchase purchase Lisinopril no prescription cheap maxalt overnight no script mastercard accepted arimidex without prescription or membership no perscription arimidex order online Aricept without prescription overnight Aricept c.o.d purchase online Aricept without rx purchase Aricept online without script Aricept uk sales Amitriptyline no prior script buy Accutane 40 mg without a rx overnight delivery Accutane 40 mg 40mg buy Accutane 40 mg in india purchase Accutane 40 mg money purchase online us pharmacy 40 mg Accutane fedex 40 mg Accutane overnight without a prescription purchase Amitriptyline pay pal without rx buy Amitriptyline with american express purchase Accutane 40 mg on line no rx buy generic Accutane 40 mg canada buy brand Accutane 40 mg buy Accutane 40 mg without a perscription purchase 40 mg Accutane without a rx online 40 mg Accutane online no perscription fedex

WP SlimStat

site tracking with Asynchronous Google Analytics plugin for Multisite by WordPress Expert at Web Design Jakarta.