0

I have the following javascript code inside index.php:

$(function () {
     $('.checkall').click(function () {
          $(this).find(':checkbox').attr('checked', this.checked);
     });
});

I retrieve information via ajax and create a table from that information using jQuery Data Tables, inside data tables head I have a checkbox with class='checkall'.

Why doesn't the checkbox that is inside the head check all other checkboxes retrieved via ajax?

*I get not errors inside Firebug

*I checked How to check every checkboxes in a jquery datatable? but still don't get it ... :(

3 Answers 3

1
$(function () {
     $('.checkall').click(function () {
          $(':checkbox').not(this).attr('checked', this.checked);
     });
});

works if there is no other checkbox on the page besides your head checkbox and the ones you want to check

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

1 Comment

works now, I added the code directly onto the element: onclick="$(\':checkbox\').not(this).attr(\'checked\', this.checked);"
1

I think that your problem is probably with your use of $(this), which probably references the 'checkall' checkbox.

find() finds descendents of the node, and the checkboxes that you want to check are not descendants of that checkbox.

The example that you cite shows the correct way to do it, by referenceing datatable.fnGetNodes()

Comments

0

In addition to what GreyBeardedGeek said, if you are loading via ajax, if your checkall checkbox is being created via an ajax call and is inserted into the DOM, then your click event will NOT fire. In this case, you should use "on" (or "live" depending on your version of jQuery): http://api.jquery.com/live/. Example:

$(document).on("click", ".checkall", 
  function(){ $(this).find(':checkbox').attr('checked', this.checked); });

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.