Untitled
unknown
javascript
2 years ago
896 B
19
Indexable
let tocLinks = document.querySelectorAll(".tocitem");
tocLinks.forEach(function(link) {
let divElement = document.createElement("div");
divElement.innerHTML = link.innerHTML;
divElement.dataset.id = "prop";
divElement.dataset.target = link.getAttribute("href").substring(1);
link.parentNode.replaceChild(divElement, link);
});
function scrollToSection() {
tocLinks.forEach(function(link) {
link.classList.remove("active");
});
let targetId = this.getAttribute("data-target"); // Get the target ID from data-target
let targetSection = document.getElementById(targetId);
if (targetSection) {
targetSection.scrollIntoView({
behavior: "smooth"
});
}
this.classList.add("active");
}
document.querySelectorAll(`[data-id="prop"]`).forEach(function(link) {
link.addEventListener("click", scrollToSection);
});Editor is loading...