views:

959

answers:

3

Hey,

I want to use a JQuery "check all" function in a form like this: http://jetlogs.org/jquery/jquery_select_all.html

My problem is I am generating my form from a php script, and I don't know exactly how many checkboxes I will have in advance. I use the "array" naming convention to be able to get all the selected checkbox values in my $_POST data... so my checkboxes are like that:

<input type="checkbox" name="items[]" value="<?php echo $id ?>">

but this JQuery declaration does not work:

$("input[@name=items[]]").each(function()    
{    
    this.checked = checked_status;    
});

probably because the "[]" at the end of my "items" messes up the JQuery declaration... I tried to write @name=items[] like that: "@name=items[]", and to put some anti-slash before the [ and ] so they're not considered as special characters but it didnt work...

If someone knows a solution and is willing to share it'd be great!! :)

+3  A: 

Escape internal brackets with '\ \' (no space) - it should fix the problem.

This selector works for me:

$('input[name=items\\[\\]]')
rochal
nope that does not work, as I said in my post: "...and to put some anti-slash (= \ ) before the [ and ] so..."
Piero
check my edit - you should remove @ from your selector and use double backslashes
rochal
YupYup!! Works like a charm! Thanks to all for your help!The other proposed solutions might also work! But This one seems the easiest to implement! :)
Piero
don't use the name attribute to do things like this! and your code is getting super unreadable!
Kennethvr
he asked why his declaration didn't work, I gave straight, correct answer. What is your problem? And yes, it is a lot better to remove name attribute, and put all checkboxes inside one div and do something like $('#container input') - but it wasn't part of his question.
rochal
I have around 300 checkboxes in this form related to different objects, organized in a tree view, and I tell you it's not easy to keep the code clean between php, Jquery and html... :-/I will try to move all the checkboxes into a div and do as you suggested, but so far this solution works fine for me...Thanks again for your help!
Piero
@Rochal, I have no problem at all, I just want people to not only solve their problem, but also to wright code that is correct. In this case the name attribute is used in the wrong way. @Piero, indeed try that solution or working with classes.
Kennethvr
+1  A: 

First of all, your name attribute should be used to set +1 elements with the same name. It's practically the same as the id attribute and should be unique per element excepted for references, but that is not the case for you.

Try using the class attribute, it's made for the things you want to do!

another thing is that in your code, you can only set your checkboxes to 'checked', but never to uncheck, this code will set your checkboxes to checked true or false, according to what it gets as attribute from the clicked element.

you can do something like this:

set your check all checkbox with an id

 <input type="checkbox" id="checkAll">

Then set on all checkboxes that should be checked/unchecked a class

<input type="checkbox" class="checked">

And the you can do something like this in jQuery

$("#checkAll").click( function(){
 var checkedValue = $(this).attr("checked");
 $("input.checked").attr("checked", checkedValue); });

this will change all checkboxes with class checked to the same checked attribute as the one with id checkAll

Kennethvr
+1  A: 

Try using

$('input[name="items[]"]')

No need for @ and use ". Note that the selector outside quotes are '. Otherwise you might cause parse error.

Emil Ivanov