473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getElementsByName doesn't return the element's value

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[i].type == "checkbox")
// collection.value = collection.value + inputElements[i].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[i].type == "checkbox")
collection.value = collection.value + inputElements[i].name + ",";
}
collection.value =
collection.value.substr(0,collection.value.length-1);
alert(collection.value);
}

Mar 7 '07 #1
2 23224
On 7 Mar, 11:58, "zephyr" <marc.at.comp...@gmail.comwrote:
>
function storeCheckboxFieldNames(theForm){
var inputElements = document.getElementsByTagName("input");
var collection = document.getElementById("checkBoxesCollection");
for(i=0; i<inputElements.length; i++){
if(inputElements[i].type == "checkbox")
collection.value = collection.value + inputElements[i].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.

Mar 7 '07 #2
Thanks, that was the solution!

Mar 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon | last post by:
<html> <head> <script language="vbscript"> sub window_onload msgbox document.getElementsByName("name1").length end sub </script> </head> <body> <div name="name1">testDiv</div>
21
by: briggs | last post by:
<html> <head> <script> /* AddChild */ function ac() { var c; var p = document.getElementById("p"); for (var i = 0; i < 5; i++) { c = document.createElement("DIV"); // Create 'div' element.
1
by: acord | last post by:
Hi, I want to use document.getElementsByName('name').value to retrieve a value of the field 'name', but it returns undefined. But function document.xxxform.name.value returns the correct value....
2
by: André Wagner | last post by:
I'm trying to get all the "divs" that have a given NAME using getElementsByName(). For example, the following code: <html> <head> <script type="text/javascript"> function on_load() { var...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
4
by: david.kuczek | last post by:
I got the following error in a javascript I wrote. The script works fine, but why is the error being displayed??? ##### Here comes the little script: function...
4
by: Otto Wyss | last post by:
I've tried to access the value of a selected option through the following getSelectValue function, passing the name of the select function getSelectValue (name) { var sel =...
2
by: tsalm | last post by:
Hi everybody, I need to get a element create dynamically by its name. It's ok for FF, but on IE, getElementsByName return "undefined". I create this code but not sure that it can work on older...
2
Frinavale
by: Frinavale | last post by:
I'm attempting to intercept the click event for RadioButons in an attempt to ask the user for confirmation of their action. I determined that Internet Explorer does not reset the previous...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.