talideon.com

I’m not shopping here, I’m going to Azerbaijan!

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!

Technorati Search Technorati Search Irish Bloggers

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.

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.