Javascript Getting specific element (of parent) by name

Posted by Fluidbyte on Stack Overflow See other posts from Stack Overflow or by Fluidbyte
Published on 2012-12-16T20:59:59Z Indexed on 2012/12/16 23:03 UTC
Read the original article Hit count: 93

I'm using custom tags to define sections in an application, so I have something like this:

<mysection>

   <form>
       <input name="myfield">
   </form>

</mysection>

I'm using the following and able to get the tag (printed to console, everything is groovy)

var parent = document.getElementsByTagName('mysection');

The issue I'm having is finding the child field by name:

var myfield = parent.getElementsByName("myfield");

...as I don't want to pick up on any other 'sections' that might have an input with the name 'myfield'.

EDIT:

var parent = document.getElementsByTagName('mysection')[0];

was suggested and returns to console the section contents, however, getElementsByName throws an error:

Uncaught TypeError: Object #<NodeList> has no method 'getElementsByName' 

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about getelementsbyname