Javascript Get child by name

I am passing var el

to a function. el

contains the previously captured element (using getElementById), and when I console.log el

in a function, I get this:

enter image description here

The problem comes when I try to grab an element internally el

using:

el.getElementsByName('fname');

      

I am getting the error:

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

      

+3


source to share


1 answer


The API getElementsByName()

is at the object level document

. This is not an HTMLElement method.

You can use instead querySelectorAll()

:



var fnames = el.querySelectorAll('[name=fname]');

      

However, it is not supported in older browsers.

+13


source







All Articles