Using jQuery to Show/Hide Form Elements Based on a Checkbox Selection

I have been hankering (whatever that means) to use jQuery since I saw it a month or so ago. So, when a friend was working on a form that had a checkbox to show/hide other form elements, I figured I'd give it a shot.

Turned out to be pretty easy and the code is short and easy to read. Here's the demo and below is the JavaScript that makes it happen

            
    <script type="text/javascript" src="jquery-latest.pack.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
       
        //Hide div w/id extra
       $("#extra").css("display","none");

        // Add onclick handler to checkbox w/id checkme
       $("#checkme").click(function(){
       
        // If checked
        if ($("#checkme").is(":checked"))
        {
            //show the hidden div
            $("#extra").show("fast");
        }
        else
        {     
            //otherwise, hide it
            $("#extra").hide("fast");
        }
      });
   
    });
     </script>

Technorati Tags:

16 Comments

  1. Luka
    Posted January 30, 2007 at 5:56 am | Permalink

    jQuery is great but without useful hints and "how to start" is not that user friendly. Thanks

  2. Posted January 30, 2007 at 9:20 am | Permalink

    Agreed. The new web site is a good move in the right direction. Once I understand it a bit more, I'll try to get something together to put on the site with more of the getting started type things.

  3. Posted September 7, 2007 at 7:22 am | Permalink

    Thanks for this Chris, I've just used it on a project.

  4. Posted January 7, 2008 at 12:01 pm | Permalink

    Thanks for the post -- I've been starting to use jQuery quite a bit in projects but the way it handles checkboxes has been slightly doing my head in.

    This is exactly the kind of example I've been looking for.

  5. Posted April 16, 2008 at 10:06 am | Permalink

    Thanks Chris. This works great…but if the user checked and then refreshes the page the div doesn't show. Check value "state" would be great. Please let me know if you have a way around that. :)

  6. Posted April 16, 2008 at 10:17 am | Permalink

    @Lorrie: you can use the jQuery cookie plugin and store the state of the checkbox in the cookie. Then, on document ready, check for the cookie value and do the show/hide as appropriate. If you did this, you'd want to break out the click handler for the checkbox into a separate function so you could reuse it to call from both the click handler and your cookie checking code.

    Hope this helps.

  7. Posted March 4, 2009 at 2:31 am | Permalink

    great tips! I just use it in my project ! thanks!

  8. jeff
    Posted April 2, 2009 at 4:07 pm | Permalink

    I just used this in a project on both check boxes and radio buttons. It was very helpful. Thank you.

  9. Posted July 14, 2009 at 2:47 pm | Permalink

    Thanks. As an additional observation, using slideDown and slideUp or simply slideToggle can also achieve a desired effect. It sometimes plays smoother without bumping elements around it.

  10. Posted August 17, 2009 at 12:59 pm | Permalink

    Great help -- Thanks.

  11. Posted August 24, 2009 at 6:20 pm | Permalink

    Hi!

    Great! This is just what I need for a project! =)

    Just 1 question, though…

    suppose you have 2 checkboxes, and want to only show one of them. For ex, option A and option B, and I want to do it so that whenever any option is selected, the other checkbox is disabled, so that only one checkbox can be checked.

    How could I do this?

    Thanks!

    Cheers from Cancun,Mexico! =)

    Camilo Olea

  12. Posted September 16, 2009 at 11:23 am | Permalink

    instead of using this:

    $("#checkme").click(function(){
    // If checked
    if ($("#checkme").is(":checked")) {
    //show the hidden div
    $("#extra").show("fast");
    } else {
    //otherwise, hide it
    $("#extra").hide("fast");
    }
    });

    you can simply use:

    $("#checkme").click(function(){
    $("#extra").toggle();
    });

  13. william
    Posted November 28, 2009 at 9:42 pm | Permalink

    THX alot!!

    I tried many websites solutions, but yours just seem to work the first time.. thx

  14. Posted December 7, 2009 at 9:06 pm | Permalink

    Great script. I executed it here:
    http://www.prova.fm/writeup/in.....p;Itemid=7

    writeup

    I turned the form into 4 checkboxes that each reveals a hidden div. I was hoping you could check & see if there's a cleaner way to do the code. Here's what I did:

    $(document).ready(function(){

    //Hide div w/id extra
    $("#maghide").css("display","none");
    $("#newsphide").css("display","none");
    $("#bloghide").css("display","none");
    $("#otherhide").css("display","none");

    // Add onclick handler to checkbox w/id checkme
    $("#magazine").click(function(){

    // If checked
    if ($("#magazine").is(":checked"))
    {
    //show the hidden div
    $("#maghide").show("fast");
    }
    else
    {
    //otherwise, hide it
    $("#maghide").hide("fast");
    }
    });

    $("#newspaper").click(function(){
    if ($("#newspaper").is(":checked")) { $("#newsphide").show("fast"); }
    else { $("#newsphide").hide("fast"); }
    });

    $("#blog").click(function(){
    if ($("#blog").is(":checked")) { $("#bloghide").show("fast"); }
    else { $("#bloghide").hide("fast"); }
    });

    $("#other").click(function(){
    if ($("#other").is(":checked")) { $("#otherhide").show("fast"); }
    else { $("#otherhide").hide("fast"); }
    });
    });

    (I compacted the 3 new checkbox calls).

    Thanks again,
    -David

  15. Posted May 7, 2010 at 11:41 am | Permalink

    $("#checkme").click(function(){
    // If checked
    if ($("#checkme").is(":checked")) {

    Simply use $(this):
    $("#checkme").click(function(){
    // If checked
    if ($(this).is(":checked")) {

  16. Posted July 2, 2010 at 9:20 am | Permalink

    Very nice, here you are the example without JQuery :

    function toggle(divId) {
    var divArray = document.getElementsByTagName("div");
    for(i = 0; i < divArray.length; i++){
    if(divArray[i].className == divId){
    if(divArray[i].style.display != 'none'){
    divArray[i].style.display = 'none';
    }else{
    divArray[i].style.display = '';
    }
    }
    }
    }

    See Descriptions

    Hello This is #1
    Hello This is #2
    Hello This is #3
    Hello This is not one of them

Post a Comment

Your email is never shared. Required fields are marked *

*
*