View Full Version : Push method and multiple checkboxes
jsolo
05-25-2010, 07:43 PM
Hello,
I have a form containing several checkboxes. The user can select 1 or many. I'm trying to populate a string variable (selections) with the checkboxes selected via the push method. Any idea what I am doing wrong here?
Many thanks,
J
var inputs = document.getElementsByTagName("productFinderForm");
var cbs = []; //will contain all checkboxes
var checked = []; //will contain all checked checkboxes
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
cbs.push(inputs[i]);
if (inputs[i].checked) {
checked.push(inputs[i]);
}
}
}
var selections = checked.push(inputs[i]);
alert(selections);
The grouped HTML elements form a DOM collection, not a real array. Unlike arrays, collections do not support some arrays methods. push() is one of them.
On the other hand, getElementsByTagName() method referes the elements with the same tag name, and I don't think you are using tags like <productFinderForm></productFinderForm>, as you have no XML there. I guess that "productFinderForm" is the name of the form. In this case, you code should be something like:
var cbs=[];
var checkedcbs=[]; // avoid using reserved words to name functions and variables
var elems=document.forms['productFinderForm'].elements, i=0, e;
while(e=elems[i++]){
if(e.type=='checkbox'){
cbs[cbs.length]=e;
if(e.checked){
checkedcbs[checkedcbs.length]=e;
}
}
}
What the array selections should contain?
jsolo
05-26-2010, 01:14 PM
Hello Kor,
Many thanks for your reply. I'm trying to populate a string variable with a concatenation of the checkboxes selected. For example, if the user selected checkboxes 1,2,4 out of the 5 then my final output would look like:
var selections = "checkbox1,checkbox,2,checkbox4"
Many thanks,
J
Could be:
function checkedC(){
var cbs=[];
var checkedcbs=[];
var selected=[];
var elems=document.forms['productFinderForm'].elements, i=0, e, j=0;
while(e=elems[i++]){
if(e.type=='checkbox'){
j++;
cbs[cbs.length]=e;
if(e.checked){
checkedcbs[checkedcbs.length]=e;
selected[selected.length]='checkbox'+j;
}
}
}
alert(selected);
}
Dormilich
05-26-2010, 02:15 PM
The grouped HTML elements form a DOM collection, not a real array. Unlike arrays, collections do not support some arrays methods. push() is one of them.
to be honest, a HTMLCollection/NodeList does not support any of the Array methods. (there is only 1 method (item()), which is required by the DOM interface, but hardly used in JS, though other languages do)
to be honest, a HTMLCollection/NodeList does not support any of the Array methods. (there is only 1 method (item()), which is required by the DOM interface, but hardly used in JS, though other languages do)
Well yes. In fact when I wrote that I was not quite attentive: jsolo was trying to create an new array formed by HTML elements, which is a different issue. Anyway, for me, I prefer to use the length of the array as index instead of the push() method. It is just a matter of choice :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.