The problem
Note: I am not sure if I understand your question correctly. If I misunderstood, please correct me, and I will edit the answer.
If in understand your question correctly, you want to create a JavaScript function which takes a string with an HTML code (e.g. '<div>abc</div>'
) as an argument and creates the corresponding DOM elements. The string can contain any HTML code (e.g. divs, spans, tables, etc.). Moreover, you do not want the elements to be created at once but sequentially in a loop.
Furthermore, you do not want to use the innerHTML
property to create all of the elements at once and then iterate over all the elements using getElementsByTagName("*")
method because you feel that it is too slow.
Answer
I think that the JavaScript function you want to create could be created but it would be a VERY complex function. You would first need to identify the individual elements (possibly using regular expressions to find opening and closing tags) including their respective classes and other attributes (such as id, name, href, etc.). Then you would need to create the individual DOM elements in a loop.
I think that much simpler, safer (less prone to errors), and more clear way would be to use the innerHTML
property and the getElementsByTagName("*")
method. I do not know why you think that the method is very slow. According to this test, getElementsByTagName("*")
is not slow at all as you can run millions of these operations in a single second. I think that your custom complex JavaScript function would probably be much slower because it would have to complete all of the steps described in the previous paragraph.
If you decide to use the innerHTML
property and the getElementsByTagName("*")
method, the code could look like this:
function parse_html_string(html_string){
let parent_div = document.createElement("parent_div");
parent_div.innerHTML = html_string // create DOM elements inside a parent div
all_elems = parent_div.getElementsByTagName("*") // get all elements inside the parent div
for (let i=0, max=all_elems.length; i < max; i++) {
console.log(all_elems[i].nodeName) // do whatever you need with each element, e.g. print its type to console
}
document.body.appendChild(parent_div);
}
var html_string = '<div>abc<span class="abc">123</span>567</div>'
parse_html_string(html_string)
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="code.js"></script>
</body>
</html>
However, if you really want to create your custom function, you can try to read through the source code of JourneyApps's domparser and use it to create your own custom function.
Additional notes
I do not understand, why you would use the loops and switch
statements in your example JavaScript code. Your JavaScript code could be rewritten simply as:
let parentDiv = document.createElement("div");
parentDiv.appendChild(document.createTextNode("abc"));
let el1 = document.createElement("div");
el1.setAttribute("class","abc");
el1.appendChild(document.createTextNode("123"));
parentDiv.appendChild(el1);
parentDiv.appendChild(document.createTextNode("567"));