Skip to content

Instantly share code, notes, and snippets.

@st3phan
Last active December 26, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st3phan/7120803 to your computer and use it in GitHub Desktop.
Save st3phan/7120803 to your computer and use it in GitHub Desktop.
Nested checkboxes. Do not return parents checkboxes when it has child checkboxes checked.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<ul id="thingy">
<li><span><input type="checkbox" checked /></span>
<ul>
<li><span><input type="checkbox" /></span></li>
<li><span><input type="checkbox" checked /></span></li>
<li><span><input type="checkbox" /></span></li>
</ul>
</li>
<li><span><input type="checkbox" /></span></li>
<li><span><input type="checkbox" checked /></span>
<ul>
<li><span><input type="checkbox" /></span></li>
<li><span><input type="checkbox" checked /></span>
<ul>
<li><span><input type="checkbox" /></span></li>
<li><span><input type="checkbox" checked /></span></li>
<li><span><input type="checkbox" checked /></span></li>
</ul>
</li>
<li><span><input type="checkbox" /></span></li>
<li><span><input type="checkbox" /></span></li>
</ul>
</li>
<li><span><input type="checkbox" checked /></span></li>
</ul>
</body>
</html>
$('input:checked').filter(function() {
if (!$(this).closest('li').find('> ul input').length) {
return $(this);
}
}).parent().css({ background: 'red' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment