Solved

Jquery - Tick/Untick checkboxes within a given tree

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…

759 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

22 Experts available now in Live!

Get 1:1 Help Now