Reputation: 23250
Using datatables. It generated following selectbox dynamically with Javascript
Firebug shows this piece of code for select box above
I tried to trigger $('#check_all').click( function()
with following simple function. But it doesn't work. What Is there any mistake in code?
$('select[name=list_length]').change(function(){
if($('#check_all').prop('checked'))
$('#check_all').click();
});
$('#check_all').click( function() {
if($(this).prop('checked')){
totalqt=0;
$('.checkbox').each(function(){
$(this).prop('checked', true);
doIt(this);
});
}else{
$('.checkbox').each(function(){
$(this).prop('checked', false);
doIt(this);
});
}
changeQt();
} );
Upvotes: 0
Views: 429
Reputation: 69905
Change your code like this.
function checkAllCheckboxes(isChecked) {
if(isChecked ){
totalqt=0;
}
$('.checkbox').each(function(){
$(this).prop('checked', isChecked);
doIt(this);
});
changeQt();
}
$('select[name=list_length]').change(function(){
if($('#check_all').is(':checked')){
$('#check_all').prop('checked', false);
checkAllCheckboxes(false);
}
});
$('#check_all').click(function(){
checkAllCheckboxes(this.checked);
});
Upvotes: 1
Reputation: 9130
I can see one thing that can be a problem in your code, but as you don't write about what exactly is failing, I'm not sure if this is the problem.
$('select[name=list_length]').change(function(){
if($('#check_all').prop('checked'))
$('#check_all').click();
});
Here, you see if the checkbox is checked, and then you invoke a click event on it.
In the click event handler, you check again if the box is checked and iterate over the other checkboxes. However, the click event you invoke in the change event handler will change the checked state of the checkbox, and thus you get the oposite result of what's intended.
How do you like this solution?
var toggleCheckBoxes = function() {
if($(this).prop('checked')){
totalqt=0;
$('.checkbox').each(function(){
$(this).prop('checked', true);
doIt(this);
});
}else{
$('.checkbox').each(function(){
$(this).prop('checked', false);
doIt(this);
});
}
changeQt();
}
$('#check_all').click(toggleCheckBoxes);
$('select[name=list_length').change(function(){
if($('#check_all').is(':checked')){ toggleCheckBoxes(); }
});
Upvotes: 0