Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post

    Adding & removing a div class when a checkbox is clicked/unclicked

    Hi guys. I have a lot of information on my site that is in multiple categories. I have a list of categories, each with its own checkbox. By default, all the information is displayed (therefore all the checkbox's are checked by default).

    When someone unchecks the box I want to hide the divs with that class, and when someone rechecks the box, they should reappear. Here is my code so far. It is hiding the div's fine, but they are not reappearing when user tics the checkbox again.

    Code:
    <script type="text/javascript">
    function toggledisplay(category){
    	if(document.getElementById(category).checked){
    		$("."+category).removeClass(category+"-hidden").addClass(category);	
    	}
    	else{
    		$("."+category).addClass(category+"-hidden").removeClass(category);		
    	}
    }
    </script>
    
    <input type="checkbox" checked="checked" name="fitness" id="fitness" onclick="toggledisplay('fitness')" />
    Last edited by inchecksolution; 08-11-2011 at 03:51 AM.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Maybe use this demo as a guide.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
            <style type="text/css">
            </style>
            <script type="text/javascript">
                function showHideDivs(obj) {
                    var infoDivsO = obj.parentNode.getElementsByTagName('div');
                    var dispStatus = (obj.checked)? 'block' : 'none';
                    for(i=0; i < infoDivsO.length; i++){
                        infoDivsO[i].style.display = dispStatus
                    }
                }
                window.onload=function(){
                    var chkCatO = document.getElementsByClassName('chkCat');
                    for(i=0; i < chkCatO.length; i++){
                        chkCatO[i].onclick = function(){
                            showHideDivs(this);
                        }
                    }
                }
            </script>
        </head>
        <body>
            <div>
                <input class="chkCat" type="checkbox" checked="checked"/> Category 1
                <div> cat 1 div 1</div>
                <div> cat 1 div 2</div>
            </div>
            <div>
                <input class="chkCat" type="checkbox" checked="checked"/> Category 2
                <div> cat 2 div 1</div>
                <div> cat 2 div 2</div>
                <div> cat 2 div 3</div>
            </div>
            <div>
                <input class="chkCat" type="checkbox" checked="checked"/> Category 3
                <div> cat 3 div 1</div>
            </div>
        </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post
    Actually, I found the answer...dumb mistake.

    If something is hidden the current class is category+"-hidden". but I was still just searching for "."+category.

    FIXED:
    Code:
    function toggledisplay(chk, category){
    	if(chk.checked == 1){
    		$("."+category+"-hidden").removeClass(category+"-hidden").addClass(category);
    	}
    	else{
    		$("."+category).addClass(category+"-hidden").removeClass(category);		
    	}
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •