selecting checkboxes

Say you have a few checkboxes, defined as below:

Item 1
Item 2
Item 3

Basic stuff. Now you want to get those that are checked.


$cb_arr = $_POST["items"];

In javascript, or jquery

var cb_arr = $(".my_cb:checked");

Notice the cb_arr above is not a js array. You need to use .each() to iterate through the items.

To pass that to php via ajax, you can serialize it like this:

var cb_arr_str = $(".my_cb:checked").map(function() { return this.value; }).get().join(',');

My point here is this. The ids turn out to be quite useless when dealing with checkboxes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s