Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
How to scroll to a certain section in a page without using href attribute

My navigation bar is self populated with javascript.

function liCreation() {
   for(noOfSection of noOfsections) {
       sectionAddress = noOfSection.getAttribute('data-nav');
       section =noOfSection.getAttribute('id');
       liTag = document.createElement('li');
       liTag.innerHTML = `<a class='menu__link'>${sectionAddress}</a>`;
}
}

So it comes out with a navigation bar menu that has 4 sections (because I got 4 sections in my page) I want to scroll to a section without adding href="${section}" to the anchor element.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Why? It gives several benefits. Href shows the arrow pointer and no code is needed to scroll

If you must, then

document.querySelector("ul.nav").addEventListener("click", function(e) {
   const tgt = e.target;
   if (tgt.tagname === "A" && tgt.classList.contains("menu__link")) {
      document.getElementById(tgt.textContent.trim()).scrollIntoView();
   }
})

assuming the id is in the text of the anchor and the ul containing the LIs have a class of .nav

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda