Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  167] [ 5]  / answers: 1 / hits: 28880  / 7 Years ago, tue, april 11, 2017, 12:00:00

How can I get all children of a html tag with a certain name?



Let's assume I have a <div> which looks like this



<div id=test>
<input name=test1/>
<input name=test2/>
</div>


if I have the div element, how can I get any child with name test1 or test2? I tried



div.getElementsByName('test1')


but that does not work?


More From » html

 Answers
13

document.querySelector('input[name=test1]')



Or if you want to target a specific child of a specific element:



var test = document.querySelector('#test')
var test1 = test.querySelector('input[name=test1]');
window.console.log(test1)

[#58190] Sunday, April 9, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrylm

Total Points: 499
Total Questions: 131
Total Answers: 108

Location: Saudi Arabia
Member since Mon, Sep 5, 2022
2 Years ago
;