cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <form name="myform" id="myform" action="" method="GET">
<ul class="tree_main">
	<li><input type="checkbox" name="grouped" id="first"><label  for="first">Item 1</label>
		<ul>
			<li><input type="checkbox" name="grouped" id="first1"><label for="first1">Item 1.1</label></li>
			<li><input type="checkbox" name="grouped" id="first2"><label  for="first2">Item 1.2</label>
				<ul>
					<li><input type="checkbox" name="grouped" id="first2ndFirst"><label  for="first2ndFirst">Item 1.2.1</label>
				</ul>
			</li>
		</ul>
	</li>
	<li><input type="checkbox" name="grouped" id="second"><label for="second">Item 2</label></li>
	<li><input type="checkbox" name="grouped" id="third"><label for="third">Item 3</label></li>
	<li><input type="checkbox" name="grouped" id="fourth"><label for="fourth">Item 4</label>
			<ul>
				<li><input type="checkbox" name="grouped" id="fourth1"><label for="fourth1">Item 4.1</label>
			</ul>
	</li>
	<li><input type="checkbox" name="grouped" id="fifth"><label for="fifth">Item 5</label></li>
</ul>
</form>
            
          
!
            
              .tree_main, ul.tree_main, ul.tree_main ul, ul.tree_main ul ul{
	list-style:none;
	margin:0;
	padding:0;
}

.tree_main label, .tree_main li{
	cursor:pointer;
}	
	
.tree_main li{
	border-bottom:1px solid #fff;
	position:relative;
}

.tree_main li:last-child{
	border:none;
}

.tree_main{
	background:#f0f0f0;
	position:relative;
}

.tree_main > li{
	padding-left:20px;
}

.sublist_level1{
	background:#d7d4d4;
	display:none;
}

.sublist_level1 > li{
	padding-left:40px;
}

.sublist_level2{
	background:#bebcbc;
	display:none;

}

.sublist_level2 > li{
	padding-left:60px;
}

.closed:before{
	position:absolute;
	left:5px;
	top:5px;
	width: 0;
	height: 0;	
	content:"";
	border-top: 5px solid transparent;
	border-left: 5px solid #000;
	border-bottom: 5px solid transparent;
}

.open:before{
	position:absolute;
	left:5px;
	top:5px;
	width: 0;
	height: 0;	
	content:"";
	border-top: 5px solid #000;
	border-left: 5px solid transparent;
	border-right: 5px solid transparent;
}
            
          
!
            
              $(document).ready( function(){	
		//add specific classes to each UL depth
		$(".tree_main > li").children("ul").addClass("sublist_level1");
		$(".tree_main ul ul").addClass("sublist_level2");
		
		//find closed items and add icon to parent if its in a UL add it to the LI parent BEFORE the hidden LI
		$(".tree_main *").children(":hidden").parent("ul").parent("li").addClass("closed");		
		
		//add specific classes unique to each LI in case specific styling needed
		$(".tree_main li").each(function (i) {
			$(this).addClass("item_" + i);
		});
		
		$(".tree_main").click(function(event) {
			var target = event.target;
			//hasClass rather than attr class as attr class matches to all not just one
			if($(target).closest("ul").hasClass("tree_main")||$(target).closest("ul").hasClass("sublist_level1")){
				$(target).children("ul").toggle();
				//only if the clicked items contains a UL show arrow
					$(target).has("ul").toggleClass("closed");	
					$(target).has("ul").toggleClass("open");	
			}
		});
		
	});
            
          
!
999px
Loading ..................

Console