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

147
Visualizações
Accessing the link inside the <li> tag using javascript

I have a navbar with an unordered list with structure of <a></a> tag inside the <li></li>. This is what it looks like:

document.querySelector('.nav-links').addEventListener('click', function(e) {
  console.log(e.target);
  e.preventDefault();

  if (e.target.classList.contains('nav-link')) {
    const id = e.target.getAttribute('href');
    console.log(id);
    document.querySelector(id).scrollIntoView({
      behavior: 'smooth'
    });
  }
});
<ul class="nav-links">
  <li class="nav-item">
    <a href="#section-1" class="nav-link">Home</a>
  </li>
  <li class="nav-item">
    <a href="#section-2" class="nav-link">Products</a>
  </li>
  <li class="nav-item">
    <a href="#section-3" class="nav-link">Benefits</a>
  </li>
  <li class="nav-item">
    <a href="#section-4" class="nav-link">About</a>
  </li>
  <li class="nav-item">
    <a href="#section-5" class="nav-link">Contact</a>
  </li>
</ul>

I can access the tag directly even if i clicked the outside of the link like in the screenshot below:

enter image description here

but the problem is that i need the value on the href so i can still change the view of my website when the

  • tag is clicked and not only when the tag is clicked. is there any way i can achieve this?

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

    0

    You can check if the element clicked was the link, otherwise find it:

    document.querySelector('.nav-links').addEventListener('click', function(e) {
      console.log(e.target);
      e.preventDefault();
      const link = e.target.classList.contains('nav-link') ? e.target : e.target.querySelector('a');
      const id = link.getAttribute('href');
      console.log(id);
      document.querySelector(id)?.scrollIntoView({
        behavior: 'smooth'
      });
    });
    .box::before { content: attr(id); }
    
    .box {
      background-color: #f0f0f0;
      height: 50vh;
      margin: 5px;
    }
    <ul class="nav-links">
      <li class="nav-item">
        <a href="#section-1" class="nav-link">Home</a>
      </li>
      <li class="nav-item">
        <a href="#section-2" class="nav-link">Products</a>
      </li>
      <li class="nav-item">
        <a href="#section-3" class="nav-link">Benefits</a>
      </li>
      <li class="nav-item">
        <a href="#section-4" class="nav-link">About</a>
      </li>
      <li class="nav-item">
        <a href="#section-5" class="nav-link">Contact</a>
      </li>
    </ul>
    <div class="box" id="section-1"></div>
    <div class="box" id="section-2"></div>
    <div class="box" id="section-3"></div>
    <div class="box" id="section-4"></div>
    <div class="box" id="section-5"></div>

    about 4 years ago · Juan Pablo Isaza Relatório

    0

    Instead of binding the click event to ul you can bind that to li.

    function onLinkClicked(e) {
      e.preventDefault();
      if (e.target.classList.contains('nav-link')) {
        const id = e.target.getAttribute('href');
        document.querySelector(id).scrollIntoView({ behavior: 'smooth' });
      }
    }
    Array.from(document.querySelectorAll('.nav-item'))
      .forEach((node) => node.addEventListener('click', onLinkClicked))
    .box {
      width: 100%;
      height: 200px;
      margin: 5px;
      background: yellow;
    }
    <ul class="nav-links">
      <li class="nav-item">
        <a href="#section-1" class="nav-link">Home</a>
      </li>
      <li class="nav-item">
        <a href="#section-2" class="nav-link">Products</a>
      </li>
      <li class="nav-item">
        <a href="#section-3" class="nav-link">Benefits</a>
      </li>
      <li class="nav-item">
        <a href="#section-4" class="nav-link">About</a>
      </li>
      <li class="nav-item">
        <a href="#section-5" class="nav-link">Contact</a>
      </li>
    </ul>
    <div class="box" id="section-1">Home Section</div>
    <div class="box" id="section-2">Products Section</div>
    <div class="box" id="section-3">Benefits Section</div>
    <div class="box" id="section-4">About Section</div>
    <div class="box" id="section-5">Contact Section</div>

    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