TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
CheckBox with Treeview using JQuery
Karthikeyan K
Sep 21
2015
Code
6.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
HTML
<ul id=
"treeList"
>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Root
<ul>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Child
</li>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Child
<ul>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Sub child
</li>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Sub child
</li>
</ul>
</li>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Child
<ul>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Sub child
</li>
<li>
<input type=
"checkbox"
name=
"selectedRole"
> Sub child
</li>
</ul>
</li>
</ul>
JQuery
$(
'#treeList :checkbox'
).change(function() {
$(
this
).siblings(
'ul'
).find(
':checkbox'
).prop(
'checked'
,
this
.
checked
);
if
(
this
.
checked
) {
$(
this
).parentsUntil(
'#treeList'
,
'ul'
).siblings(
':checkbox'
).prop(
'checked'
,
true
);
}
else
{
$(
this
).parentsUntil(
'#treeList'
,
'ul'
).each(function() {
var $
this
= $(
this
);
var childSelected = $
this
.find(
':checkbox:checked'
).length;
if
(!childSelected) {
$
this
.prev(
':checkbox'
).prop(
'checked'
,
false
);
}
});
}
});
Live demo
:
http://jsfiddle.net/KarthikeyanK/jgjft83w/
treeview
JQuery
Checkbox