So I know that getElementById
is available with the document object.
const ELEMENT_BY_ID = document.getElementById('header'); // works fine
Now what I am trying to do is to use getElementById
on an HTML node instead of the document object.
const PARENT_ELEMENT = document.getElementById('parent');
const CHILD_BY_CLASS_NAME = PARENT_ELEMENT.getElementsByClassName('ch'); // Works fine
const CHILD_BY_ID = PARENT_ELEMENT.getElementById('child1'); // Error PARENT_ELEMENT.getElementById is not a function
Here with PARENT_ELEMENT
I am able to use getElementsByClassName
or Tag Name but don't know why getElementById
is not available here. Is there a reason why getElementsByClassName
is available but getElementById
is not available? Below is my HTML
<div id="parent">
<span id="child1" class="ch ch1">Child</span>
<span id="child2" class="ch ch3">Child</span>
<span id="child3" class="ch ch3">Child</span>
</div>