getElementsByName doesn't return the element's value

Discussion in 'Javascript' started by zephyr, Mar 7, 2007.

  1. zephyr

    zephyr Guest

    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);
    }
    zephyr, Mar 7, 2007
    #1
    1. Advertising

  2. zephyr

    wisestpotato Guest

    On 7 Mar, 11:58, "zephyr" <> wrote:
    >
    > 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.
    wisestpotato, Mar 7, 2007
    #2
    1. Advertising

  3. zephyr

    zephyr Guest

    Thanks, that was the solution!
    zephyr, Mar 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    document.getElementsByName problem

    simon, May 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    26,246
    simon
    May 10, 2005
  2. Greenhorn
    Replies:
    15
    Views:
    792
    Keith Thompson
    Mar 6, 2005
  3. AR
    Replies:
    2
    Views:
    183
  4. mitch
    Replies:
    3
    Views:
    250
    Richard Cornford
    Nov 14, 2005
  5. briggs
    Replies:
    20
    Views:
    445
    Randy Webb
    Jan 17, 2006
Loading...

Share This Page