views:

218

answers:

2

I have a form with image thumbnails to select with checkboxes for downloading. I want an array with the images in jQuery for an Ajax call.

2 questions:
- On the top of the table there is a checkbox to toggle all checkboxes that I want to exclude from the mapping. I had a look at jQuery's .not() but I can't implement it with the :checkbox selector
- is the following example code correct?

$(document).ready(function() {
    $('#myform').submit(function() {
        var images = $("input:checkbox", this).map(function() {
            return $(this).attr("name");
        }).get().join();

        alert(images); // outputs: ",check1,check2,check3"
        return false; // cancel submit action by returning false
    });
}); // end doc ready

HTML:

    <form id="myform" action="" >
    <input type="checkbox" id="toggleCheck" onclick="toggleSelectAll()" checked="checked" ><br />

    <input type="checkbox" name="001.jpg" checked="checked" /><br />
    <input type="checkbox" name="002.jpg" checked="checked" /><br />
    <input type="checkbox" name="003.jpg" checked="checked" /><br />
    <br />
    <input type="submit" value="download" >
</form>
+2  A: 

You can exclude it via the ID, like this:

$("input:checkbox", this).not("#toggleCheck").map(....

This would exclude the select all toggle from the mapping.

Nick Craver
Thanks (again) Nick.Now, I had my checkboxes all checked by default but when I deselect some, they don't update the DOM.. how should I do this?
FFish
@FFish - Not sure I understand completely, but if you want to map only the checked ones, add to the selector, like this: `$("input:checkbox:checked", this)`
Nick Craver
Supah, your da man. Cheers!
FFish
A: 

You can chain like the following

    $('input:checkbox:not(#toggleall)').after('this is selected');

see the example code here http://jsbin.com/ofecu and http://jsbin.com/ofecu/edit

calumbrodie