How to Change the href Value of an <A> Tag After the Link Click using jQuery?

by | September 20, 2022 | Javascript, Website Development

This page may contain affiliate links to recommended products or services. If you choose to purchase after clicking a link, I may receive a commission at no additional cost to you.

Last updated on: February 8, 2023

How to change the href value of an <a> tag after the link click using jQuery:

$(document).on('click', 'a', function(e) {
  e.preventDefault();

  // Simulate a link click to a new URL.
  window.location.href = "http://floydhartford.com";
});

Every time an <a> tag is clicked jQuery will simulate changing the href value after the link has been clicked. Technically, it’s not actually changing the href value. Instead, jQuery is changing the final destination of the end-result of clicking the link — it updates the href value to point to the URL of the new website where the person who clicked the link should be sent. So, it’s the same result as changing the href value of the <a> tag, but a different approach to achieve it.

Fore more javascript web page redirect solutions and an in-dept discussion on the topic refer to StackOverflow Question 503093 “How Do I Redirect to Another Webpage?

Floyd Hartford is a website developer from southern Maine. He's focused on creating and building WordPress websites and loves spending time digging into code like HTML, CSS, scss, jQuery, PHP, and MySQL.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

five × three =

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Looking for WordPress Help?

I’m a freelance web developer specializing in small business and corporate marketing websites.

Pin It on Pinterest