Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-11-2011, 02:03 AM   PM User | #1
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
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 02:51 AM..
inchecksolution is offline   Reply With Quote
Old 08-11-2011, 02:50 AM   PM User | #2
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
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>
bullant is offline   Reply With Quote
Old 08-11-2011, 02:52 AM   PM User | #3
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
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);		
	}
}
inchecksolution is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:56 PM.


Advertisement
Log in to turn off these ads.