Example of code:
var new_node = document.createElement('div'); new_node.id = 'parent_1'; var html = '<div id="select_1">hey</div>'+ '<div id="select_2">hey</div>'; new_node.innerHTML = html; example of code with DOMParser
var parser = new DOMParser(); new_node.appendChild(parser.parseFromString(html, "text/html").documentElement); in whatever way, I cannot access to the element through
console.log(document.getElementById('select_1')); //null // but I can with the DOM-like node (appended to the document) console.log(document.getElementById('parent_1')); //correct What options do I have? I would not want to write the select_x nodes in a DOM-like style, since html code written in that way would lose all understand-ability IMHO
*The parent_1 node is appended onto a real HTML node such as document.getElementById('html_element').parentElement.appendChild(parent_1);
EDIT: solved By using querySelector, for example
var parser = new DOMParser(); var temp = parser.parseFromString(html, "text/html").documentElement; console.log(temp.querySelector('#select_1')); //correct new_node.appendChild(temp);
if="and notid="is that just a typo or the real issue here?getElementById('select_1')is selecting a non-existent element even if it were appended to the document.