Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello all. I am facing some strange issue with JQuery. Let me explain it in detail.

I got some data from DB and based on it i generated parent child checkboxes. I have single div on page. I generate one div for every parent child bunch and attach each of the genrated div to the div that is already on the page. So after doing all this, i got something similar to this one : Example image[Not actual one]

Now,see the image. I have similar layout.You can consider that Each fruits category with all the childs are under one div.

What i am trying is,Check all functionality. if i check Parent checkbox,all the children will get checked. Its working fine,the issue is its working only first time. If i uncheck and recheck again(parent),it doesnt checks all the children. I tried to debug it in browser. It shows checked in Browser,but in UI its unchecked.The code is as shown below:
C#
for (k = 0; k < ParentCount; k++) {
           $("#parentchk" + k).on('click', function () {
               if ($(this).is(':checked')) {//if parent is checked
                   $(this).parent().find('input:checkbox').each(function () {//get the Div for this bunch and loop through all the checkboxes
                       $(this).attr('checked', 'checked');
                   });
               }
               else {
                   $(this).parent().find('input:checkbox').each(function () {
                       $(this).removeAttr('checked');
                   });
               }
           });
       }

This code is under the function which dynamically generates the Checkboxes. I am unable to identify the cause of issue.

Anyone there who can identify? Let me know if you need more details or anything is still not clear.
Posted
Updated 4-Jun-14 0:40am
v2
Comments
Thanks for the question Rohan. Nicely explained. Now let me reproduce the issue at my end. :)
Thanks7872 4-Jun-14 7:36am    
If you still need to play around with this one let me know. :-). I will give you the HTML.
Give me. :)
Thanks7872 4-Jun-14 7:48am    
<div id="functions" style="clear: both; height: 250px; width: 950px;">
<div id="appended_div0" style="float: left;">
<input id="Parentchk0" value="3" class="parents" type="checkbox"><input id="Parentlbl0"
value="Center Of Excellence" type="label" style="font-size: 20px; border: none;"><br>
<input id="Childchk0" value="4" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl0" value="Pre sale" type="label" style="border: none;"><br>
<input id="Childchk7" value="14" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl7" value="Innovation" type="label" style="border: none;"><br>
<input id="Childchk8" value="15" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl8" value="Patenting" type="label" style="border: none;"><br>
<input id="Childchk9" value="16" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl9" value="Competency Development" type="label" style="border: none;"><br>
<input id="Childchk16" value="23" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl16" value="Project Managment" type="label" style="border: none;"><br>
</div>
<div id="appended_div1" style="float: left;">
<input id="Parentchk1" value="5" class="parents" type="checkbox"><input id="Parentlbl1"
value="Delivery" type="label" style="font-size: 20px; border: none;"><br>
<input id="Childchk1" value="8" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl1" value="Development" type="label" style="border: none;"><br>
<input id="Childchk2" value="9" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl2" value="Project Analysis" type="label" style="border: none;"><br>
<input id="Childchk3" value="10" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl3" value="Testing" type="label" style="border: none;"><br>
<input id="Childchk4" value="11" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl4" value="Design" type="label" style="border: none;"><br>
<input id="Childchk5" value="12" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl5" value="BPO" type="label" style="border: none;"><br>
<input id="Childchk6" value="13" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl6" value="Print Media" type="label" style="border: none;"><br>
</div>
<div id="appended_div2" style="float: left;">
<input id="Parentchk2" value="7" class="parents" type="checkbox"><input id="Parentlbl2"
value="Human Resource" type="label" style="font-size: 20px; border: none;"><br>
<input id="Childchk12" value="19" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl12" value="Recruitment" type="label" style="border: none;"><br>
<input id="Childchk13" value="20" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl13" value="Skill & Talent Pool Management" type="label" style="border: none;"><br>
<input id="Childchk14" value="21" class="childrens" type="checkbox" style="margin-left: 20px;"><input
id="Childlbl14" value="Salaries & Reimbursements" type="label" style="border: none;"><br>
<input id="Childchk15" value="22" class="ch
Can you just give me the HTML of the Total Div, so that I can try this?

I solved it by replacing the code snippet with below one.
C#
$(document).on("click", "INPUT[id^=parentchk]", function (event) {
var targetId = event.target.id;
var parentdvId = $(this).parent().attr('id');

if ($("#" + targetId + ":checked").length > 0) {
$("#" + parentdvId + " input:checkbox").each(function () {
$("#" + parentdvId + " input:checkbox").prop("checked", true);
});
}
else {
$("#" + parentdvId + " input:checkbox").each(function () {
$("#" + parentdvId + " input:checkbox").prop("checked", false);
});
}
});
 
Share this answer
 
Comments
Nirav Prabtani 4-Jun-14 7:40am    
Congrats.. :)
Nelek 4-Jun-14 7:47am    
You forgot to indent your code ;P
Thanks7872 4-Jun-14 8:52am    
Unfortunately i am not able to do it right now. I think this page has some bugs. Not sure. Do you see any changed fonts on whole page?
Nelek 4-Jun-14 9:20am    
yes, there must be a "code" somewhere without a "/code" tag, that's why all the following text without "pre" tags get red and big
Nelek 4-Jun-14 9:21am    
You should post your code snippets into question and/or answer, then delete the comments with snippets. Probably it get ride of the problem
I found the problem. In your first attempt, you are using attr() and removeAttr() methods.
Refer - Add attribute 'checked' on click jquery[^].
Quote:
In IE, this will always actually make the checkbox checked. In other browsers, if the user has already checked and unchecked the checkbox, setting the attribute will have no visible effect. Therefore, if you want to guarantee the checkbox is checked as well as having the checked attribute, you need to set the checked property as well:

Then in your next attempt, you corrected the code to deal with the checked property instead of attribute.
JavaScript
$("#" + parentdvId + " input:checkbox").prop("checked", true);

$("#" + parentdvId + " input:checkbox").prop("checked", false);

As I was debugging, suddenly it came to my mind that, when the code ran for the first time, it removed the attributes all together from the CheckBoxes, so how it would work next time. Then I researched more and find out that we should set the property instead of attribute.
 
Share this answer
 
v2
Comments
Thanks7872 4-Jun-14 8:54am    
+5..! Great Great catch. And thats how i will improve my JQuery skills :) Thanks alot. I reported the bug related to Fonts.
Most welcome bro. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900