Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: how to get values from all selected checkboxes from a table

I've searched in many threads but i don't see any checkboxes that involves tables, i just wanted to make deleting easy for users, Say, i have a table with fifty entries and i wanted to delete 10 all in one go, so i select the check box beside each record so when i want to delete it it will just get the values of the checkbox pass it on to a php script..

My question is how do i im[plement this on Javascript or jQuery?. getting the values from n numbers of checkboxes? since tables have a dynamic value depending on how many it has on the database.

Here is an image to be clear:

enter image description here

like image 659
lemoncodes Avatar asked Jun 14 '26 05:06

lemoncodes


2 Answers

This will give you an array containing the value attribute of each checked box:

var values = $('input:checked').map(function() {
    return this.value;
}).get();

See http://jsfiddle.net/p58Hw/1/

like image 100
Alnitak Avatar answered Jun 16 '26 19:06

Alnitak


Select your table, find all rows with a checked checkbox, store their values, and remove them:

var $chkboxes = $(yourTable).find("tr input[type='checkbox']:checked");
var checkBoxVals = $chkboxes.map(function(){ return $(this).val(); }).toArray();

$chkboxes.closest('tr').remove();

// serialize array checkBoxVals and pass it to your php script
like image 41
nbrooks Avatar answered Jun 16 '26 19:06

nbrooks