0

I want to add an Enable all checkbox in an HTML <table>, that toggles all checkboxes inside that <table>.

I can't get it to work.

My Jquery :

var elementName = 'TestName';

$('input[familyname="TestName"]').on('click', function() {
    if ($("input[familyname='" + elementName + "']").is(':checked')) {
        $(this).find('tr').nextAll('tr').find('input').prop('checked', true);
    } else {
        $(this).find('tr').nextAll('tr').find('input').prop('checked', false);
    }
});

See also this Fiddle.


Update:

I got it to work with this code:

var elementName = 'TestName';
var $checkboxes = $('.table-list').find('input'); 

$('input[familyname="TestName"]').on('click', function() {
    $checkboxes.prop('checked', $(this).is(':checked'));
});

However, there is bug when I want to add more tables. See this Fiddle.

4 Answers 4

2

You have to change your code like below,

var elementName = 'TestName';
$('input[familyname="' + elementName + '"]').on('click', function() {
  var elems = $(this).closest('table.table').find('tr input');
  elems.prop('checked', this.checked)
});

tr is parent in our context, so .find() over $(this) will not work out. So we have to use .closest() to grab the parent table and from that we can fetch all the required input - check box elements.

DEMO


You have same id set with two radio buttons, give unique id to radio button and use the above code.

DEMO

Sign up to request clarification or add additional context in comments.

6 Comments

It works but if i want to have 2 tables in same time?
@EdinPuzic It would be better to answer if you show the entire html
@EdinPuzic Just give different id to that enable all check box and its label. Check this demo. jsfiddle.net/5m5a60um/28
@EdinPuzic By the way, Try to press the tick mark inside anyone of the answers here. As it will help the future visitors to spot a worked out answer for them. And this is not a compulsion. :)
Thank you very very much!! :)
|
1

This is a simple but optimal way to achieve the desired effect, that works for any number of checkbox groups :

var elementName = 'TestName';

$('input[familyname="' + elementName + '"]').on('click', function(e) {
    $(this)
        .closest('.table-list')
        .find('input')
        .prop('checked', this.checked);
});

(see also this Fiddle)

8 Comments

It works but if i want to have 2 tables in same time like this jsfiddle.net/5m5a60um/25
@EdinPuzic : Just add some IDs and it works fine. Also, make sure all of your IDs are unique! Check out this Fiddle here --> jsfiddle.net/5m5a60um/26
@JohnSlegers Will you create 100 event handlers if there are 100 tables? :\
@JohnSlegers It works but the probles is that I will have more then 30 tables on one page.
@EdinPuzic Why can't you use the approach given here
|
1

If you are putting in thead then you can't just directly do .find() instead traverse to tbody and select all input with type=checkbox, like:

 $(this).closest("table").find('tbody input[type=checkbox]').prop('checked', true);

Similarly do:

 $(this).closest("table").find('tbody input[type=checkbox]').prop('checked', false);

3 Comments

It works but if i want to have 2 tables in same time?
I dont understand, what do you mean by 2 tables in same time? @EdinPuzic
Like this, I have different 2 tables with enable all button jsfiddle.net/5m5a60um/27 and I need to separate enable all button. Now when i click on table number 1 it enables table number 2 also
0

Try this :

$(document).on('click', '#switchall1', function() {

if ($(this).is(':checked')) {
    $(this).closest('table').find('tbody tr input').prop('checked', true);

}
else {
    $(this).closest('table').find('tbody tr input').prop('checked', false);
}

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.