talideon.com

Blackout Ireland

May 17, 2005 at 5:15PM The DOM HTMLFormElement Interface Should Have a getElementsByName() Method

While useful on the HTMLDocument interface, getElementsByName() would be much more useful if it was included in HTMLFormElement interface. Every time I want to look up elements by their names, it’s usually on form elements rather than the document as a whole. Come to think of it, I don’t think there’s ever been a time I wanted to look elements up by name on the document, whereas I’ve wanted to do it on forms quite a bit. What a pain.

Oh, and here’s some code to get the same effect.

function GetElementsByName(frm, name) {
    var elements = [];
    for (var i = 0; i < frm.elements.length; i++) {
        if (frm.elements[i].name == name) {
            elements[elements.length] = frm.elements[i];
        }
    }
    return elements;
}

It’s trivial, but hey!

Comments

1 On May 31, 2005 at 17:35, Jimmy Wilson wrote:

form.elements[name] or form[name] will return a filtered collection of all form elements with the matching name.

2 On June 2, 2005 at 15:35, Keith wrote:

Really? I thought the subscript as restricted to numbers.

3 On June 7, 2005 at 19:15, Keith wrote:

Jimmy, you’re right. I’d something that required me to do this, so I tried it to check if you were right, and it works.

Thanks for the correction.

4 On August 29, 2006 at 12:28, Franck wrote:

But Jimmy forgot to mention the fact that document.forms[my_form].elements will ignore elements that have been dynamically added to the form via DOM manipulation.