Javascript Get Child Element by Name

一笑奈何 提交于 2019-12-07 15:37:53

问题


I'm passing a var el into a function. el contains previously grabbed element (using getElementById) and when I console.log el in the function I get the following:

The problem comes in when I try to grab an element inside of the el using:

el.getElementsByName('fname');

I get the error:

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

回答1:


The getElementsByName() API is at the document object level. It's not an HTMLElement method.

You could use querySelectorAll() instead:

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

It's not supported in older browsers however.



来源:https://stackoverflow.com/questions/14842951/javascript-get-child-element-by-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!