Well, you already have a link to it using the element variable.
But I bet you mean that you want something inside. If it is right, you can use getElementsByTagName('*') , then iterate over the returned collection by checking the ID property until yours is found.
var div = document.createElement('div'); div.innerHTML = '<p>yo</p><div><span id="tester">hi</span></div>'; var all = div.getElementsByTagName('*'); // search for element where ID is "tester" for (var i = 0, len = all.length; i < len; i++) { if (all[i].id === 'tester') { var result = all[i]; break; } } alert( result.id );
Before you try the loop, you can check if querySelectorAll is querySelectorAll in the browser and use that if it is.
if( div.querySelectorAll ) { var result = div.querySelectorAll('#tester'); } else {
source share