Posted on 9th November 2009No Responses
How to process multiple checkboxes with jQuery

The functionality isn’t new: you have a list of stuff and want checkboxes before the elements so you can mass-process them. In my case, I wanted to set a number of list elements to a certain state (which in this case means a status flag in the database) so the need arose to fetch all selected elements (where the is ), stuff them into an function and deselect the items afterwards.

For this example, my list looks like:

<input type="" value="1" name="check[]" /> 1<br />
<input type="" value="2" name="check[]" /> 2<br />
<input type="" value="3" name="check[]" /> 3<br />
<input type="" value="4" name="check[]" /> 4<br />

<button>send</button>

Now I want to get alle checkboxes with and process them with . For this task, I employ ’s each() functionality:

$('button').click(function(){
  $('::').each(function(){
    $.post('process.php', { : $(this).attr('value') });
  });
});

Now what does that do?

$('::').each(function(){

tells to get all checkboxes and via each() do something (function()…)

$.post('process.php', { : $(this).attr('value') });

For each value that we get, we want to send an call to process.php and pass the current value as parameter “”, which we can pick up with PHP. If you like, you can also add a callback function to the call (e.g. an alert which says “Thanks I’ve used your data” or something alike).

Share this on:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • email
  • MisterWong
  • MySpace
  • Reddit
  • RSS
  • Yahoo! Bookmarks

Related posts

Comments
Leave a Response
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img src="">