Friday, December 19, 2008

Tri-State Checkbox using Javascript

Recently I noticed how yahoo mail select all check box only shows two
states while selecting/un-selecting mails to perform various actions.
So I decided to experiment and create a simple tri-state check box that could display my intermediate state. Below is what I ended up with:

Tri-State Checkbox Demo with dependent checkboxes

Select/Unselect All






It's really simple to use. Here's what you have to do:
  1. Include the javascript file in your html
  2. Have a place-holder node (span in my case) to hold the image for the tri-state box
  3. Place the related checkboxes inside a container, e.g. a div
  4. Invoke the initTriStateCheckBox(<place holder id>, <container id>, false) function in your html

Standalone Tri-State Checkbox Demo

I liked this
(Maybe/Yes/No)

Here's what you have to do to create a standalone one:
  1. Include the javascript file in your html
  2. Have a place-holder node (span in my case) to hold the image for the tri-state box
  3. Create a hidden field to hold the state of the box
  4. Invoke the initTriStateCheckBox(<place holder id>, <hidden field id>, true) function in your html


The zip file containing the javascript, images and a sample is available here. It is also a GitHub project set up by Michal Letynski.

--

41 comments :

[hide] Unknown said...

I must say it's a sly implementation :-D Was really got me thinking about from where the intermediate box was displaying... and a inspecting it with the firebug explained all the tricks :-D

on December 19, 2008 at 10:06 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7572514067374607452.0
  • @Unknown
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

damn firebug gave it all away :D

on December 19, 2008 at 12:09 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @298163183294147842.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Pointless Researcher said...

Very interesting. I would like to see an implementation that does not need child nodes at all and it just cycles through the states on a click

on December 19, 2008 at 3:26 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @991013112619236168.0
  • @Pointless Researcher
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

I have updated the post to include an example of a standalone one also now :)

on December 19, 2008 at 11:06 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @130452472572654626.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Pointless Researcher said...

I've compiled an alternative (following a simple naming convention) to include it in IWebMvc2:

function changeCheckboxState(chk) {
var img = byId(chk.id);
var src = img.src.substring(img.src.lastIndexOf("/") + 1);
if (checkboxImages[0] == src) {
byId("_" + chk.id).value = "";
img.src = checkboxImagesPath + checkboxImages[1];
} else if (checkboxImages[1] == src) {
byId("_" + chk.id).value = "true";
img.src = checkboxImagesPath + checkboxImages[2];
} else {
byId("_" + chk.id).value = "false";
img.src = checkboxImagesPath + checkboxImages[0];
}
}

<img id="id" src="/demo/images/unchecked.gif" onclick="changeCheckboxState(this)" /><input id="_id" type="hidden" value="false" />

So thanks for the work! I was looking for a similar workaround so it came handy ;-)

on December 19, 2008 at 11:40 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5009017766772374021.0
  • @Pointless Researcher
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

thanks. i would prefer not having to declare the image explicitly and let the script create the image.

i had a look at a couple of videos of iwebmvc. it looks cool, really easy to crud entities. where do you use it?

on December 20, 2008 at 12:31 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @687934611824129403.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

It would be nice to have multiple levels of nestings.

on February 3, 2009 at 6:32 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6381151318299345257.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

it should be easy to do. You need to modify the script a little bit to handle the scenario of a nested tri-statebox along with the checkboxes.

on February 3, 2009 at 7:57 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6078536946193863342.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

What modifications would need to be done?

on February 3, 2009 at 7:21 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7012269602712731824.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

on February 3, 2009 at 10:31 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6890186105621465004.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

I don't think you could do it with custom attributes on the span you are creating. Because you have to walk up and down the hierarchy from the given tristatebox you are at. I'm not sure there is an easy way to wrap that all up.

on February 3, 2009 at 10:31 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6890186105621465004.1
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

ok i'll look into it later today. just need to manage a way to instruct the checkboxes to also update any tri-states in the ancestry

on February 3, 2009 at 11:00 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4349756475143063873.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] fearphage said...

My suggestion is to use a single image for all states and move it to the correct location. This technique is referred to as css sprites. You can see google using the sprites technique as well.

on February 7, 2009 at 6:09 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @1417280941803828149.0
  • @fearphage
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

agreed.

on July 4, 2009 at 5:29 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @366909728040232819.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Unknown said...

“Dan orang-orang yang beriman dan berhijrah serta berjihad pada jalan Allah (untuk membela Islam) dan orang-orang (Ansar) yang memberi tempat kediaman dan pertolongan (kepada orang-orang Islam yang berhijrah itu), merekalah orang-orang yang beriman dengan sebenar-benarnya. Mereka beroleh keampunan dan limpah kurnia yang mulia.” (Al-Anfaal 8:74)

Salam ziarah dari saya,,blog yang menarik ,jemput muzakarah di blog saya….dan jadikan blog saya sebagai rakan anda..tq

on April 14, 2009 at 1:35 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @375765996661049212.0
  • @Unknown
is the first line of your comment.
Click here to enter your reply
[hide] Rizky Ikhsani said...

I have a problem. Do you mind if I post my script and ask for your help to edit it? I have a problem with step 3

on June 9, 2009 at 11:20 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7724909267454523655.0
  • @Rizky Ikhsani
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

sorry for the late reply. feel free to email me explaining the problem ur facing

on July 4, 2009 at 5:29 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @74520460137969982.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Joel said...

Shams, just wanted to let you know that I created a JSF tri-state-checkbox. I would like to include your checkbox images in a downloadable package with my checkbox implementation from my blog. I already have a link to your blog entry on my site. Are you ok with that? http://digitaljoel.blogspot.com/2009/07/tri-state-checkbox.html

on July 7, 2009 at 11:12 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2898298704497544934.0
  • @Joel
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

sure go ahead. fearphage had a suggestion about using sprites. maybe you can implement that too to save on requests to load the image

on July 7, 2009 at 11:39 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6452385279428870513.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Joel said...

yeah, I saw that sprite suggestion and looked into it a while back but probably won't be doing it this iteration. Thanks!

on July 7, 2009 at 11:53 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4193201433080273561.0
  • @Joel
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

It is extremely interesting for me to read the article. Thank author for it. I like such themes and everything connected to this matter. I definitely want to read a bit more soon.

on November 22, 2009 at 7:37 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5160433526891239092.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

Great article you got here. I'd like to read more concerning that matter. Thnx for posting this data.
Sexy Lady
Escorts UK

on December 10, 2009 at 7:18 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5653458504637083319.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

uh. thank you for this style )

on December 16, 2009 at 2:17 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5758302631205868411.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Daniel said...

Hi Shams,

I sent you an e-mail (guessed at your address) with some extensions to your tristateCheckbox. I just want to be sure that the e-mail reached you.

on September 2, 2010 at 8:56 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6422356244119797129.0
  • @Daniel
is the first line of your comment.
Click here to enter your reply
[hide] Margo said...

Cool post as for me.
I'd like to read something more about that matter.
Thanks for giving that info.
With best regards Margo!
hotel Kiev escort

on September 18, 2010 at 1:52 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @9158091558764854806.0
  • @Margo
is the first line of your comment.
Click here to enter your reply
[hide] Xander Dumaine said...

I have some jquery functions which change the values of the inputs they get changed, but the images don't refresh until after I mouse over them. Any idea how to get the images to refresh as soon as the value changes?

on August 2, 2011 at 9:34 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4880821313148643569.0
  • @Xander Dumaine
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

Are the checkboxes on this page working for you? Can you share a demo?

on August 2, 2011 at 10:28 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @815809098012590926.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Xander Dumaine said...

Basically I have a collection of dynamically loaded independent tri-state checkboxes, and I want to have a the "set all" buttons, but I want them each to be different, and be able to set to the checked/unchecked/ and intermediate. It's in the sidebar here

on August 2, 2011 at 9:12 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3404233698289009940.0
  • @Xander Dumaine
is the first line of your comment.
Click here to enter your reply
[hide] Xander Dumaine said...

@Shams its up at http://drinkthebeergames.com . For the most part, it is working rather well.

on August 2, 2011 at 8:51 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7522848113333364975.0
  • @Xander Dumaine
is the first line of your comment.
Click here to enter your reply
[hide] Deepak Chaudhary said...

This comment has been removed by the author.

on August 24, 2011 at 7:06 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8932204583380976793.0
  • @Deepak Chaudhary
is the first line of your comment.
Click here to enter your reply
[hide] Deepak Chaudhary said...

Is there any way to manually change the status of tristateBoxInput (checkbox image)?
I have a reset button which which clears all the checkboxes using javascript. But the state of tristateInputBox remain unchanged?

I tried setting its value to 0 as below.
document.getElementById('tristateBoxInput').value=0;
also
document.getElementById('tristateBoxInput').checked=false;

but these did not change the state of tristateInputBox

on August 24, 2011 at 7:08 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @1800370591751769272.0
  • @Deepak Chaudhary
is the first line of your comment.
Click here to enter your reply
[hide] Chris said...

Fantastic work Shams! Many thanks. Just wondering - where in the javascript should I add a callback function? I've tried a few places, including the end of updateStateAndImage, and it has broken my app :P if you could help me find the right place I'd really appreciate it. Thanks again for the great work!

on December 5, 2011 at 7:08 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6962107284929336102.0
  • @Chris
is the first line of your comment.
Click here to enter your reply
[hide] Chris said...

Further to this - tried more ways to introduce a callback to the JS and couldn't manage it. For my app though, adding an onmouseout event to the span element worked.
Thanks again Shams!

on December 6, 2011 at 3:20 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8547335449099256472.0
  • @Chris
is the first line of your comment.
Click here to enter your reply
[hide] Chris said...

This comment has been removed by the author.

on December 6, 2011 at 3:20 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8914200472677422093.0
  • @Chris
is the first line of your comment.
Click here to enter your reply
[hide] Willie said...

I recently came across the blog again and remembered I made a prototype version of this, it also supports itself (dependant tri-state elements), anyway here is a link

on August 30, 2012 at 6:38 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7816837624357069047.0
  • @Willie
is the first line of your comment.
Click here to enter your reply
[hide] Robson said...

Hello!!!
I need a quad-state checkbox to use on a tree.
My tree have to check the root and maintain it checked since the children are checked or not.
And if there is a child checked, its father have to be checked and show that there is a child checked in the minimum.
Is there a quad-state checkbox with this feature?

on August 16, 2013 at 4:01 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2608207810480042178.0
  • @Robson
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

Can you explain how it is different from the tri-state checkbox that is there currently. What is your fourth state?

on August 16, 2013 at 1:38 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6478313655796169199.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Robson said...

The fourth state is a mixed between "clicked" and "maybe".
I need to know if a father was clicked and some of his sons didn't.
In my system, I can to choose a father, but not necessarily all sons.
And I can choose all of his sons, but not him.
I will try to make an ascii.

[x] - Checked
[-] - Maybe
[ ] - Unchecked
[+] - Mix between Checked and Maybe.

[+] Father choosen
| - [ ] Son not choosen
| - [x] Son choosen

[-] Father not choosen
| - [x] Son choosen
| - [x] Son choosen.

All choosen.

[x]
| - [x]
| - [x]

Nobody choosen is easy to see.

Thanks

on August 27, 2013 at 12:35 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5301391227239442386.0
  • @Robson
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

I think you can add the father as an independent tri-state checkbox. Then it can be in any of the three states irrespective of the state of the 'son' checkboxes.

on August 27, 2013 at 11:55 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3481697648130193766.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Robson said...

Almost.

See this, please. http://www.indigorose.com/webhelp/vp/Program_Reference/Misc/Quad-state_Check_Box.htm

The father needs to know a state of his sons.

on August 28, 2013 at 4:05 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @749792607218018606.0
  • @Robson
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

There are some problem with IE8. I have this structure:
[ ]
- [ ]
- [ ]
- [ ]

If I check top level check box all children change their states to checked, but top level check box Value field still have zero value.
topLevel.Value = 0

on September 24, 2013 at 3:20 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4189049285450388189.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anna Schafer said...

In IE10 the behavior was reverted back to the IE8 behavior and onerror should will always be called.”psd to wordpress

on November 15, 2015 at 1:58 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8140976239496068966.0
  • @Anna Schafer
is the first line of your comment.
Click here to enter your reply