3

let's start from the beginning, but please don't panic... is a Jquery issue ;-)

I writing a PHP code: the user sends a query to our database and the form shows sets of results, grouped in tables, with checkboxes to select the data.

I would like to know how to enable a Jquery code to run with a Select/Deselect checkbox, to Select/Deselect the data of each table, i mean, each set of results.

For example, something like this example http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/

1: All checkboxes have a class of “checkbox”: input type=”checkbox” class=”checkbox”

function toggleChecked(status) {
    $(".checkbox").each( function() {
        $(this).attr("checked",status);
    });
}

Could you tell me what is the best way to modify the toggleChecked function?

A second parameter to call toggleChecked, to replace ".checkbox" with the second parameter value? (i'm unable to find the correct syntax to do it).

Or there is a different way to do it?

And, BTW, is better to use prop instead attr? Best regards.

kinakuta
  • 9,029
  • 1
  • 39
  • 48
  • Yes, better to use prop(). And here is something that should have turned up in your search: http://stackoverflow.com/questions/5229023/jquery-check-uncheck-all-checkboxes-with-a-button – Jay Blanchard Dec 03 '12 at 16:41
  • Maybe i'm confused, but is not exactly the same, right? All code examples are for just one set of checkboxes. I don't know how select a certain group of checkboxes or #slaves checkboxes... – David García Aristegui Dec 03 '12 at 16:49
  • You take that concept and apply it to checkboxes that you have designated to be part of a group. – Jay Blanchard Dec 03 '12 at 16:51

4 Answers4

4

Just add a second parameter to the function, and use it.

function toggleChecked(status,selector) {
    $(selector).each(......);
}

If you are honestly struggling with something as simple as this, you really shouldn't be using jQuery yet. You should learn about plain JavaScript first. Otherwise it's like going to a construction site with the best tools in the world, maybe even the knowledge of how to use those tools perfectly... but with no idea how basic construction works, you'd probably make a beautiful building with no foundations that would collapse within a day.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
3

Example

jQuery.is really helps in the case or prop("checked") will return a boolean (so either way):

var boxes = $(".checkbox");
boxes.prop("checked", !boxes.prop("checked"));

And yes, jQuery.prop is better.

Joe
  • 80,724
  • 18
  • 127
  • 145
0

Well, I know you already got the answer, but... here is my version:

$.fn.changeCheckboxesState = function(state){
        if (state === undefined)
            state = true;

        $(this).filter(':checkbox').each(function() {
            $(this).prop('checked', state);
        });

        return this;
    };
$.fn.toggleCheckboxes = function() {

        $(this).filter(':checkbox').each(function() {
            var $this = $(this);

            $this.prop('checked', !$this.prop('checked'))
        });

        return this;
    };
Michael Malinovskij
  • 1,422
  • 8
  • 22
0
<script>
$(document).ready(function() {
    $('#selecctall').click(function(event) {  
        if(this.checked) { 
            $('.checkbox1').each(function() { 
                this.checked = true;           
            });
        }else{
            $('.checkbox1').each(function() { 
                this.checked = false;                   
            });        
        }
    });

});
</script>

In Cakephp code--

<?php   echo $this->Form->input('selecctall',array('id'=>'selecctall','type'=>'checkbox')); ?>

<?php   echo $this->Form->input('massaction',array('class'=>'checkbox1','label'=>'','type'=>'checkbox')); ?>
princespn
  • 31
  • 7