Question: JAVASCRIPT HTML Hello, I need help turning my code into a HTML file so it can populate a webpage. Please add html codes. CODE let
JAVASCRIPT HTML
Hello, I need help turning my code into a HTML file so it can populate a webpage. Please add html codes.
CODE
let sourceDoc = document.getElementById("source_doc");
let toc = document.getElementById("toc");
let headingCount =1;
const heading = "H2";
for (let n = sourceDoc.firstElementChild; n !== null; n = n.nextElementSibling) {
if (n.nodeName === heading) {
let anchor = document.createElement("a");
anchor.name = "doclink" + headingCount;
n.insertBefore(anchor, n.firstElementChild);
let listItem = document.createElement("li");
let link = document.createElement("a");
listItem.appendChild(link);
link.textContent = n.textContent;
link.href = "#" + "doclink" + headingCount;
toc.appendChild(listItem);
headingCount++;
}
}
// Append the table of contents to the page
document.body.appendChild(toc);
document.body.appendChild(sourceDoc);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
