Title: Get parent checkbox value if its parent is not checked
Tags: javascript;jquery
Question: I would like to get the value of the parent checkbox ONLY if it is checked and its parent checkbox is not. For example, If i check Manager 1, I should only get Manager 1. Same goes with the President. But if I check Assistant Manager 1 to 3, I should only get Assistant Manager 1 to 3.
I have this JS but I'm stuck with this line if($(txtbox).is(':checked')==false). It always returns true even though the parent is not checked.
Here is the JS snippet:
$('#xxx').on('click',function () {
var hid = new Array();
var counter = 0;
$('ul li input[type=checkbox]').each(function () {
if($(this).is(':checked')){
var chk = $(this);
li = chk.closest('li');
ul = li.parent();
parentli = ul.parent();
if($(parentli).prop("tagName")=="LI"){
txtbox = parentli.find('input');
alert($(txtbox).is(':checked'));
if($(txtbox).is(':checked')==false){
hid[counter] = chk.val();
counter++;
}
}else{
hid[counter] = chk.val();
}
}
Here is the html:
<ul&gt;
<li&gt;<label&gt;<input type="checkbox" value="Administration" /&gt;Administration</label&gt;
<ul&gt;
<li&gt;<label&gt;<input type="checkbox" value="President" /&gt;President</label&gt;
<ul&gt;
<li&gt;<label&gt;<input type="checkbox" value="manager 1" /&gt;Manager 1</label&gt;
<ul&gt;
<li&gt;<label&gt;<input type="checkbox" value="asst manager 1"/&gt;Assistant Manager 1</label&gt;</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="asst manager 2"/&gt;Assistant Manager 2</label&gt;</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="asst manager 3"/&gt;Assistant Manager 3</label&gt;</li&gt;
</ul&gt;
</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="manager 2"/&gt;Manager 2</label&gt;</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="manager 3"/&gt;Manager 3</label&gt;</li&gt;
</ul&gt;
</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="Vice President"/&gt;Vice President</label&gt;
<ul&gt;
<li&gt;<label&gt;<input type="checkbox" value="Manager 4"/&gt;Manager 4</label&gt;</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="Manager 5"/&gt;Manager 5</label&gt;</li&gt;
<li&gt;<label&gt;<input type="checkbox" value="Manager 6"/&gt;Manager 6</label&gt;</li&gt;
</ul&gt;
</li&gt;
</ul&gt;
</li&gt;
</ul&gt;
<input type="button" id="xxx"  value="click" /&gt;
DEMO: http://jsfiddle.net/Er6Rx/1/

Here is the accepted answer: $(txtbox).prop('checked')
Instead of
$(txtbox).is(':checked')

Here is another answer: I'm not sure what it is you're doing with all this code but from what I read it sounds like this is what you want:
http://jsfiddle.net/Er6Rx/8/
$('#xxx').on('click',function(){
var selected = '';
$('input[type=checkbox]').each(function () {
var sThisVal = (this.checked ? "1" : "0");
if(sThisVal==true)
{
selected += '<li&gt;'+$(this).attr('value')+'</li&gt;';
}
});
$('.checkedboxes').append('<ul&gt;'+selected+'</ul&gt;');

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/21/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><jquery>', 'Title': 'Get parent checkbox value if its parent is not checked', 'ViewCount': 767, 'id': 22552703, 'user_AboutMe': None, 'user_AccountId': 4092359.0, 'user_DisplayName': 'user3360031', 'user_Reputation': 607.0, 'user_Views': 68.0, 'user_id': 3360031.0}}