最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - Javascript Get Child Element by Name - Stack Overflow

programmeradmin0浏览0评论

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'

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'
Share Improve this question asked Feb 12, 2013 at 22:22 FluidbyteFluidbyte 5,21010 gold badges49 silver badges79 bronze badges 2
  • I think that means getElementsByName is not a method of that object. Is el from a library like jQuery? I think you want document.getElementsByName without knowing what el is. – Leeish Commented Feb 12, 2013 at 22:23
  • is there any reason not use jQuery? – Senad Meškin Commented Feb 12, 2013 at 22:23
Add a comment  | 

1 Answer 1

Reset to default 17

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.

发布评论

评论列表(0)

  1. 暂无评论