Solved

Jquery - Tick/Untick checkboxes within a given tree

Posted on 2011-09-14
1
451 Views
Last Modified: 2012-05-12
Hello,

This question follows on from:

http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_27303621.html

Please this the update example:

Example

What I want to do is constrain the 'select all' to within the relevant tree.
For example, if the user clicks on Select All under the 'Main' tree, then only the checkboxes under Main (including any grandchildren should be checked (and vice versa).

Currently, it is also checking items in the Main2 tree.

How can I amend the code to work with this scenerio (note the sub_xxx IDs are pulled from the db and aren't static).

Many Thanks as usual!

0
Comment
Question by:jagku
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36540086
notice that the top nodes changed from:
<ul class='noBulletsWithIndent'>

to:
<ul class='noBulletsWithIndent mainNode'>
<script type="text/javascript"> 
$(function() {
	$('li.selectAll>input:checkbox').bind('click',function(){
		var root=$(this).closest('.mainNode');
		var ul=$(this).closest('ul');

		if('ALL'==this.value &&'LI'!=ul.parent().closest('ul').parent().get(0).nodeName.toUpperCase())
			$(':checkbox',root).attr('checked',this.checked);
		else
			$('>li>input:checkbox',$(this).closest('ul')).attr('checked',this.checked);
	});
 
	$('input[type=checkbox]').bind('click',function(){
		var root=$(this).closest('.mainNode');
		var ul=$(this).closest('ul');
		$('>li.selectAll>:checkbox:eq(0)',ul).attr('checked', $('>li>:checkbox:gt(0):checked',ul).size()==$('>li>:checkbox:gt(0)',ul).size() );
		$(':checkbox:eq(0)',ul).attr('checked', $(':checkbox:gt(0):checked',ul).size()==$(':checkbox:gt(0)',ul).size() );
		$(':checkbox:eq(0)',root).attr('checked', $(':checkbox:gt(0):checked',root).size()==$(':checkbox:gt(0)',root).size() );
	});
 
 
 
});
</script>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question