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

350
Views
Prevent reload page when click on button take you to the same page link?

I would like to prevent an HTML button that acts like a link take you to the same page from reload the page

For Example: prevent reload the website when click on logo if you are in Homepage.

How can I do this?

Full code: https://codepen.io/Muh-Ail/pen/oNpLKXM?editors=0010

   let activeBtn = document.querySelector(".active-btn");
   let currentLocation = location.href;

    if (activeBtn.href === currentLocation) {

        activeBtn.preventDefault();

    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use this instead

let activeBtn = document.querySelector(".active-btn");
let currentLocation = location.href;


if (activeBtn.href === currentLocation) {
    activeBtn.addEventListener('click',function(e){
        e.preventDefault();
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

You'd have to use the e.preventDefault(); from the listener ele.addEventListener("click", function (e) instead;

Also, you should wrap the js with:

document.addEventListener("DOMContentLoaded", () => {
// code here
});

You should add different links for you to be able to see.

about 4 years ago · Juan Pablo Isaza 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!