tags:

views:

415

answers:

5

i have a form with a "Select All" button and a bunch of checkboxes. i want to have it select all checkboxes when the user clicks the "Select All" button.

is there elegant way in jquery to do this?

EDIT #2: i have isolated the problem down to this code; style="display: none;". if i remove this code it works fine. any ideas?

EDIT: The answers below work in my test form but the button in this case is inside of a form that is inside of a div that only shows up as part of a simpledialog.show(). in this case, for some reason when i click on the button i dont see anything happen:

javascript:

<script type="text/javascript">
$(document).ready(function() {
    $('#sdHc3').simpleDialog({
        showCloseLabel: false,
        open: function() {
            $('#checkboxStatus').html('');
        },
        close: function() {
            var c = [];
            $('#checkboxForm :checkbox:checked').each(function() {
                c.push($(this).val());
            });
            $('#checkboxStatus').html('&nbsp;&nbsp;Checked <b>' + c.join(', ') + '</b>.').show();
        }
    });

});

<script type="text/javascript">
$(function() {
$('#selectAll').click(function() {
        var select_all = (this.value === 'Select All');
        $(':checkbox').attr('checked', select_all);
        this.value = (select_all) ? 'Deselect All' : 'Select All';
    });
});

body:

<div style="display: none;" class="scrollableDiv" id="simpleDialog3">
    <h3>DEMO3</h3>
    <form id="checkboxForm">
    <input type="button" id="selectAll" value="TT" />
    <input type="checkbox" class="chckbx" value="1" /><br />
    <input type="checkbox" class="chckbx" value="1" /><br />
    </form>
    <p>
        <a href="#" class="close">Close</a></p>
</div>

any idea why it wouldn't work in this case?

+5  A: 
$(function(){
    $('#myButton').click(function(){
        var select_all = (this.value === 'Select All');
        $('input:checkbox').attr('checked', select_all);
        this.value = (select_all) ? 'Deselect All' : 'Select All';        
    });
});

Try placing this code before your dialog instantiation.

It seems like you may want to switch to jQuery UI Dialog.

ChaosPandion
where do you put this?
ooo
The new example should be easier to work with.
ChaosPandion
I would put the $('input[type=checkbox']) selector in the this.form context. I would also use a flag instead of this.value for checking if all should be checked or not. (Also, if one checkbox is unchecked, the text isn't changed, etc.)
strager
Also also, I would use the :checkbox selector instead of checking the type, as it's cleaner-looking, as in GApple's answer.
strager
You make some good points.
ChaosPandion
this works fine on my test form, but in my new case, th3e button is inside a div that is by default hidden and then shows up inside of dialog. for some reason in this case it doesn't work
ooo
Can you give some more context in your example?
ChaosPandion
The documentation also recommends 'input:checkbox' over just ':checkbox', as using just the pseudo-selector maps to the slower value '*:checkbox'
GApple
i have updated the question with my code
ooo
have isolated the problem down to this code; style="display: none;". if i remove this code it works fine. any ideas?
ooo
@oo, Can you post your code on jsbin.com for a live example?
strager
http://jsbin.com/aduce
ooo
@oo, The dialog doesn't show. You seem to be missing that code.
strager
+2  A: 

Having your button call this when clicked will select all checkboxes on the page:

jQuery("input:checkbox").attr('checked','checked');

You can change the selector ("input:checkbox") to provide better context (and possibly speed up it's execution as well)

GApple
A: 

just give all the checkboxes a class, and attach this jQuery to the button:

$(document).ready( function () {
  $('#myButton').click( function() {
    $('.checkbox').attr('checked','checked');
  });
}

Assuming your button has the id="myButton" and your checkboxes have the class="checkbox".

BrianV
instead of ".checkbox" (assign "checkbox" as a class on all inputs that should be selected) you can use "input[type=checkbox]"
jeef3
`input:checkbox` is neater, but still could lead to unwanted checkboxes being ticked, depending on the other elements on the page.
nickf
+1  A: 

So if your select-all button has an id of select-all you say:

$('#select-all').click(function(){
   $('input:checkbox').attr('checked', 'checked');   
);
Colin
A: 

In the cases where I've wanted the same functionality, it usually is a table of rows with checkboxes down the side. In that case, you could include this code snippet and then any time there is a checkbox in a header cell, all the associated checkboxes in the column below it will be switched on/off depending on its value.

Assuming HTML like this:

<table>
    <thead>
        <tr>
            <th><input type="checkbox" /></th>
            <th>...</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" name="row[0]" /></td>
            <td>...</td>
        </tr>
        ...
    </tbody>
</table>

Here's the jQuery:

$('th input:checkbox').click(function() {
    var $this = $(this),
        colNum = $this.closest('th').prevAll('th').length,
        checked = $this.attr('checked')
    ;
    $this
        .closest('table')
        .find('tbody > tr')
        .each(function() {
            $(this)
                .children("td")
                .eq(colNum)
                .find("input:checkbox")
                .attr('checked', checked)
            ;
        })
    ;
});

It doesn't require any extra ids or classes or anything and can work for multiple columns of checkboxes, which is nice. Here's a demo page: http://jsbin.com/eqeno

nickf
if this inside a div your code doesn't seem to work. i have put an example here: http://jsbin.com/oyuha
ooo
That will be something to do with the other plugin you're using - using a `live()` event instead of `click()` would probably solve that.
nickf
what is the live() event. i tried it and it didn't seem to do anything
ooo
http://docs.jquery.com/Events/live it will apply your event handlers to elements which are added to the DOM later
nickf