Home > JQuery, Javascript, Programming > Getting selected values of an checkbox array with JQuery

Getting selected values of an checkbox array with JQuery

With JQuery it is extremely easy to collect the checked values of an checkbox array. You can use the code below to collect checked items of an array named “itemSelect[]” and give an alert if none of them are checked. This code submits the collected data via ajax to “/ajax_do_something.php” to process the data. If sucessfully processed it refreshes the page to display new data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var selectedItems = new Array();
$("input[@name='itemSelect[]']:checked").each(function() {selectedItems.push($(this).val());});
 
if (selectedItems .length == 0) 
    alert("Please select item(s) to delete.");
else
    $.ajax({
	type: "POST",
	url: "/ajax_do_something.php",
	data: "items=" + selectedItems.join('|'),
	dataType: "text",
	success: function (request) {
	    document.location.reload();
	  },
	error: function(request,error){
	    alert('Error deleting item(s), try again later.');
	  }
	}
    )

JQuery, Javascript, Programming

  1. Monica Baquerizo
    January 15th, 2010 at 21:00 | #1

    Thanks. I send my array by post. The code is:

    var selectedItems = new Array();
    $(”input[@name='grupo[]‘]:checked”).each(function() {selectedItems.push($(this).val());});

    $.post( “/c_reportes/consulta_orgXgrupo”, {’data[]‘:selectedItems}, function(data){

    $(’#output’).html(data);
    });

  2. May 13th, 2010 at 13:22 | #2

    Thank’s for the post – just saved me about an hour of working this out!

  3. ravi
    August 4th, 2010 at 08:51 | #3

    it is not working in ie8

  4. November 25th, 2011 at 16:10 | #4

    thanks i spent hours trying to figure out why jquery wouldn’t do all of my checkboxes I wish I would have found this first. Here is what i came up with

    function test(){
    var boxIds = new Array();
    $(”input:checkbox[id=boxId]:checked”).each(function() {clientIds.push($(this).val());});
    if (boxIds .length == 0)
    alert(”Please select item(s) to delete.”);
    else
    alert(”boxIds: “+boxIds.join(’|'));
    }

  1. No trackbacks yet.