• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Change the href of a Link

Using JavaScript to Change the href of a Link

January 30, 2022 Leave a Comment

To change the href of a link using JavaScript, we can use the href property along with the getElementById method.

document.getElementById("link1").href = "some-url.html";

Let’s say I have the following HTML code:

<div id="div1">
  <a id="link1" href="http://theprogrammingexpert.com/">Home Page</a>
</div>

To change the href of #link1 from http://theprogrammingexpert.com/ to http://theprogrammingexpert.com/category/javascript/, we will use the href property in the following JavaScript code:


document.getElementById("link1").href = "http://theprogrammingexpert.com/category/javascript/";

The above code would change the link from the home page to the JavaScript category page.

Using JavaScript to Change the href of a Link with a Click

To change the url of a link we can combine the href property with a click event.

Let’s say we have the same HTML from above.

<div id="div1">
  <a id="link-1" href="#test">This link will add #test to the current URL if clicked</a>
  <div id="click-me" onclick="changeLink()">Change link</div>
</div>

We have added an onclick event to the #click-me div which will run a function where we can change the link URL. We will also change the text of the new link using the textContent property.

Here is the function below that will accomplish this:

function changeLink(){
  document.getElementById("link-1").href = "#different-url";
  document.getElementById("link-1").textContent = "This link will add #different-url to the current URL if clicked";
};

The final code and output for this example of how to change the href of a link using the href property and JavaScript is below:

Code Output:

This link will add #test to the current URL if clicked

Change link

Full Code:

<div id="div1">
  <a id="link-1" href="#test">This link will add #test to the current URL if clicked</a>
  <div id="click-me" onclick="changeLink()">Change link</div>
</div>

<script>

function changeLink(){
  document.getElementById("link-1").href = "#different-url";
  document.getElementById("link-1").textContent = "This link will add #different-url to the current URL if clicked";
};

</script>

Hopefully this article has helped you understand how you can use JavaScript to change the href of a link.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Stop a Timer
  • 2.  Using JavaScript to Add Items to Set
  • 3.  Using JavaScript to Get New Date Format in dd mm yy
  • 4.  How to Split a Number into Digits in JavaScript
  • 5.  How to Use JavaScript to Round Up Numbers
  • 6.  Sum the Digits of a Number in JavaScript
  • 7.  Using JavaScript to Get a Random Number Between 1 and 100
  • 8.  Insert Character Into String In JavaScript
  • 9.  JavaScript slice – How to Get a Substring From a String
  • 10.  JavaScript Trunc with Math.trunc() Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy