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

447
Visualizações
Prevent page reload on html anchor tag

How do i prevent the anchor tag from reloading my page when i click it using js

<a href='?code=$user_code' class=''>view user</a>
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Like Barmar suggested in the comments, you can add an event listener to the anchor element that calls event.preventDefault() to prevent page reload. Inside the event listener you can fetch the data from database.

<a id="my-anchor" href='?code=$user_code'>view user</a>
var myAnchor = document.getElementById('my-anchor');

myAnchor.onclick = function(e) {
  e.preventDefault();
  
  var href = myAnchor.getAttribute('href');

  // fetch data from database
};

https://jsfiddle.net/tojx5ske/

Another option is to create an anchor with href="#" and pass the real href to the event listener using data attributes:

<a id="my-anchor" href="#" data-href='?code=$user_code'>view user</a>
var myAnchor = document.getElementById('my-anchor');

myAnchor.onclick = function(e) {
  var href = myAnchor.dataset.href;

  // fetch data from database
};

https://jsfiddle.net/tojx5ske/1/

almost 4 years ago · Santiago Trujillo 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