Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

453
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!