Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
896 B
3
Indexable
Never
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);
});