Solved

Jquery - Tick/Untick checkboxes within a given tree

Posted on 2011-09-14
1
445 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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…

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now