Solved

Jquery - Tick/Untick checkboxes within a given tree

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

785 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