Easy checkbox toggling with jQuery

So I needed to have a regular form checkbox toggle the visibility of a div in my webpage today. It turned out jQuery supports the concept of toggling visibility via .toggle(), which I didn’t realise until I looked it up. And so you can support clicking or even keyboard changing of the checkbox via the change event:


$("#checkbox").change(function() {

$("#areaToToggle").toggle();

});

Pretty sweet, obviously the div should be hidden to begin with. We could add another line after the change event:


$("#areaToToggle").hide();

Too easy :-)

Tags:

Leave a Reply