When someone clicks on first input:checkbox, I want my code to check all checkboxes, but my code doesn’t work correctly:
EXAMPLE: http://jsfiddle.net/cQYVE/
$('.table_show tr input:first').live('click',function(){
var isChecked = $(this).prop('checked');
$(".table_show tr input[name=checked[]]").each(function(){
if (!$(this).prop('checked') == isChecked) {
$(this).parent().click();
}
});
});
What is the cause of the problem?
the problem with your code is that you dont quote the name selector properly:
change
to
see:
http://jsfiddle.net/cQYVE/5/
also, your UX could be better. Usually when the user manually checks all the checkboxes, the checkall checkbox should become checked, and when user unchecks one box so that “all” arent checked, the checkall box should become unchecked
edit:
The answer to the 2nd question is that you must maintain a count of total number of checkboxes, and when that number is checked, the checkall checkbox must be checked, otherwise it must be unchecked. do this check every time user checks a checkbox manually. Sorry, i dont have the time to provide that code right now.