javascript - How get element by id with other context? -
i want understand how can element id of 'takeme' in other context not document, example:
<div id="parent"> <div id="takeme"></div> </div>
i know can use:
document.getelementbyid('takeme');
being document context, want know how can takeme parent, without using queryselectorall
, trying to:
var parent = document.getelementbyid('parent'); parent.getelementbyid('takeme');
but of course didn't work.
getelementbyid
htmlelement
not work because getelementbyid
defined document. there document.getelementbyid there not element.getelementbyid
.
as commenters have mentioned, not needed parent node since id
should unique document
.
however, other methods element.getelementsbyclassname should work different context(htmlelement
)
var parent = document.getelementbyid('parent'); var fromothercontext = parent.getelementsbyclassname('takeme'); console.log(fromothercontext); fromothercontext = parent.getelementbyid('takeme'); console.log(fromothercontext);//only works document
<div id="parent"> <div class="takeme"></div> <div class="takeme"></div> <div class="takeme"></div> <div id="takeme"></div> </div>
Comments
Post a Comment