Question

I have got created a rather complex HTMLElement and would like to know id there is a way to run a getElementById on it before the element gets appended to the DOM.

What I have tried is to call the function like this:document.getElementById.call(newElement, "id");

and like this: Document.prototype.getElementById.call(newElement, "id");

in both cases the I just get the error Uncaught TypeError: Illegal invocation

Is there a posibility to do that or is it just not possible?

Note: I am trying to solve a JS problem here, thats why I do not want jQuery solutions.

Was it helpful?

Solution

You can do

var yourElement = newElement.querySelector('#someId');

Complete example :

var newElement = document.createElement('div');
newElement.innerHTML="<div id=a>AA</div><span id=b>BB</span>";
console.log(newElement.querySelector('#b').innerHTML) // logs BB

Note : IE7 isn't supported

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top