jQuery how to check if a checkbox is checked

The goal was to get the textbox to slide up and down when the checkbox was clicked. A rather simple thing. As I don't do jQuery or so much client side coding this took me some time to find out. The solution was not so obvious at the start but after checking the jQuery documentation and doing some testing this was the result.

If you want to get the value of an attribute you should use the ":attribute" convention. In the case of the checkbox I first put the object in a variable and then used the .is keyword to do a boolean check of the value, like so.

<script type="text/javascript">
    function toggleTextBox() {
        var checkBox = $('#<%=chkMoreInfo.ClientID %>');
        var textBox = $('.txtMessageArea');
         if (checkBox.typeOf != "undefined") {
             if (checkBox.is(":checked")) {
                 textBox.slideUp("slow");
             }
             else {
                 textBox.slideDown("slow");
             }
          }
     }
</script>

So long!

Print | posted on Saturday, January 10, 2009 6:30 AM

Comments

Gravatar

# re: jQuery how to check if a checkbox is checked, Posted by north face jackets on sale on 10/27/2010 8:12 AM

The goal was to get the textbox to slide up and down when the checkbox was clicked.

MBT shoes clearance| snow boots for women | columbia jackets | mac makeup | the north face outlet
snow boots for women | columbia jackets | mac makeup | the north face outlet

Gravatar

# re: jQuery how to check if a checkbox is checked, Posted by austin church on 10/19/2011 9:03 AM

This is a wonderful tutorial blog impressed me a lot as i was looking for this one for many days.

Comments

Title: *
Name: *
Email: (never displayed)
Website:
Comment: *  
Please add 4 and 5 and type the answer here: