checkbox - Select checkboxes all only works once?

checkbox - Select checkboxes all only works once?

mattnthatmattnthat Posts: 18Questions: 0Answers: 0
edited February 2013 in General
Im using a checkbox in the table header to select (or unselect) all checkboxes in that column. Im new to JQuery.

Code in script (Run from inside $(document).ready(function() {)....

$('#check_all').click( function() {
$('input', oTable.fnGetNodes()).attr('checked',this.checked);
} );

This code works perfectly but only once! I can click it and select all AND then i can click it and it will unselect all. However from then on i click it and nothing happens.

When i chuck a console.log("Check event hit"); into the script it fires every time.. yet it only updates the checkboxes for the first check and uncheck operations.

Any thoughts?

Matt

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Use prop rather than attr? You are reading from the property but setting the attribute.

    Allan
  • mattnthatmattnthat Posts: 18Questions: 0Answers: 0
    Allan,

    Gold. Working perfectly now.

    Thanks for the tip, I got the statement based on attr from a comment you had made earlier about selecting all. I must have not got the context right.

    Thanks again and great work on data tables!

    Matt
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    > Thanks for the tip, I got the statement based on attr from a comment you had made earlier about selecting all. I must have not got the context right.

    More likely I got it wrong, or it was an old comment before the jQuery split between attr and prop. Either way, good to hear it is working now.

    Allan
  • mattnthatmattnthat Posts: 18Questions: 0Answers: 0
    Hi Allan,

    I think it was before the JQuery split! im learning!

    I dont quite have a working solution as my checkbox looks to select too much!
    Im using oTable.fnFilter( tTag,'5');

    To filter the data but the "select all" checkbox selects items that are currently filtered out?

    Is there anyway to align fnGetNodes() with what is actually on the screen?

    Thanks again for your help so far!
  • mattnthatmattnthat Posts: 18Questions: 0Answers: 0
    Ive just tried to combine a method for selecting just checked with this but i may have syntax wrong
    Was this:
    $('input', oTable.fnGetNodes()).prop('checked',this.checked);

    used a line ive seen elsewhere: .$('tr', {"filter": "applied"} );
    but this:
    $('input', .$('tr', {"filter": "applied"} )).prop('checked',this.checked);

    fails with "Unexpected identifier "
  • mattnthatmattnthat Posts: 18Questions: 0Answers: 0
    Update. Got it working. Had missed out an "oTable" reference in my last update.

    For reference:
    $('input', oTable.$('tr', {"filter": "applied"} )).prop('checked',this.checked);

    Got me going.
    Matt
This discussion has been closed.