getElementsByName doesn't return the element's value

Z

zephyr

Hi,
I have a hidden field in my form:
<input type="hidden" name="checkBoxesCollection" value="">

If I submit the form I call a javascript function:

function storeCheckboxFieldNames(theForm){
var inputElements = theForm.getElementsByTagName("input");
var collection = document.getElementsByName("checkBoxesCollection");
alert(collection[0].value);
for(i=0; i<inputElements.length; i++){
// if(inputElements.type == "checkbox")
// collection.value = collection.value + inputElements.name +
",";
}
// collection.value =
collection.value.substr(0,collection.value.length);
// alert(collection.value);
}

(I commented lines to see the effect of only the first 3 lines).

Running this function gives me an alert with the names of _all_
fieldnames in the form. I thought this should just output the value of
the first element by the name of "checkBoxesCollection" - which is ""

Can anyone explain this??

btw
If I use document.getElenmentById instead document.getElementsByName
the function works exactly as designed: concat all names of checkboxes
in the form and assign them to the value of the hidden field.

function storeCheckboxFieldNames(theForm){
var inputElements = document.getElementsByTagName("input");
var collection = document.getElementById("checkBoxesCollection");
for(i=0; i<inputElements.length; i++){
if(inputElements.type == "checkbox")
collection.value = collection.value + inputElements.name + ",";
}
collection.value =
collection.value.substr(0,collection.value.length-1);
alert(collection.value);
}
 
W

wisestpotato

function storeCheckboxFieldNames(theForm){
var inputElements = document.getElementsByTagName("input");
var collection = document.getElementById("checkBoxesCollection");
for(i=0; i<inputElements.length; i++){
if(inputElements.type == "checkbox")
collection.value = collection.value + inputElements.name + ",";
}
collection.value =
collection.value.substr(0,collection.value.length-1);
alert(collection.value);

}


Your problem appears to be that you are referencing "collection"
without specifying an index. In the above function, change each
"collection.value" to "collection[0].value", and you should be fine.

hth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top