I am trying to find all HTML tags with a specific name “name” inside an element with a specific id.
document.getElementById("myid").getElementsByName("name");
throws error getElementsByName is not a function
The sample code is:
<!DOCTYPE html>
<html>
<body>
<div id="myid">
<p name="name">first name</p>
<p name="name">first name</p>
</div>
<p id="result">RESULT</p>
<script>
let el = document.getElementById("myid")
let names=el.getElementsByName("fname");
document.getElementById("result").innerHTML = elements[0].innerHTML;
</script>
</body>
</html>
I saw somewhere that unlike other functions the ‘getElementsByName’ is limmited to ‘Document’ scope only and does not exist in any other scope.
- is this true?
- what alternative approach should I use instead of ‘getElementsByName’?