I have a form that contains a couple of checkboxes. After send this form I would need these checkboxes (the values) to a javascript array.
Could anyone give me a favor, please, how to do?
var form = document.getElementById("yourformname"),
inputs = form.getElementsByTagName("input"),
arr = [];
for (var i = 0, max = inputs.length; i < max; i += 1) {
// Take only those inputs which are checkbox
if (inputs[i].type === "checkbox" && inputs[i].checked) {
arr.push(inputs[i].value);
}
}
You can test the following function on jsfiddle:
function getCheckboxes(form_id, name) {
// returns array of checked checkboxes with 'name' in 'form_id'
var form = document.getElementById(form_id);
var inputs = form.getElementsByTagName("input");
var values = [];
for (var i = 0; i < inputs.length; ++i) {
if (inputs[i].type === "checkbox" &&
inputs[i].name === name &&
inputs[i].checked)
{
values.push(inputs[i].value);
}
}
return values;
}