DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

How Do You Update an HTML Link’s href Attribute?

Edit an anchor’s href in markup or JavaScript, and distinguish the browser-resolved URL from the literal attribute text.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To change where an HTML hyperlink goes, edit the anchor’s href attribute. In JavaScript, set the anchor’s href property or use setAttribute(). The key distinction: anchor.href returns the browser-resolved URL, while getAttribute("href") returns the literal text in the attribute.

Change a hyperlink in HTML

An <a> element creates a hyperlink, and its href attribute specifies the destination. The anchor’s visible text should make that destination clear to readers. See MDN’s reference for the anchor element.

As an Amazon Associate I earn from qualifying purchases.

To update a link in the page’s markup, replace the existing href value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<a href="/old-page">Read the guide</a>
<a href="/new-page">Read the guide</a>

An href can point to a web page, file, email address, location within the current page, or another resource addressed by a URL. The destination changes when the attribute value changes; the link text can remain the same if it still accurately describes where the link leads.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Change an anchor’s href with JavaScript

When the page is already loaded, select the anchor and assign its href property:

const link = document.querySelector("a");
link.href = "/new-page";

This updates the element’s href attribute. The selector above changes the first matching anchor; use a more specific selector if the page contains several links:

const link = document.querySelector("#help-link");
if (link) {
  link.href = "/support";
}

You can also update the attribute with setAttribute():

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
link.setAttribute("href", "/new-page");

setAttribute() adds the named attribute if it is absent and replaces its value if it is already present. For an anchor’s destination, setting link.href is usually the straightforward option; setAttribute() is useful when code needs to work with attributes generically. See MDN’s documentation for HTMLAnchorElement.href and MDN’s documentation for setAttribute().

Why does anchor.href return a full URL?

The href property reports the effective URL: for a valid relative URL, the browser resolves it against the document’s base URL and returns an absolute URL. By contrast, getAttribute("href") returns the literal attribute text.

<a id="guide" href="../new-page">Read the guide</a>

const link = document.querySelector("#guide");
console.log(link.getAttribute("href")); // "../new-page"
console.log(link.href);                 // absolute URL resolved by the browser

Use link.href when your code needs the destination the browser will navigate to. Use link.getAttribute("href") when it needs the authored string, such as the original relative path. Relative URL resolution is defined in the context of the document’s base URL; the WHATWG HTML Standard’s links section describes this behavior.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle URL input and link behavior safely

Constrain destinations from user input

Do not assume an arbitrary string supplied by a user is a safe destination. Validate or constrain URLs to the schemes and destinations your application intends to allow. MDN warns that setAttribute() can be an injection sink for certain attributes, including in script-URL contexts; use applicable Trusted Types protections where the sink supports them. These precautions matter whether the value is assigned through the property or set through an attribute.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make new tabs and downloads clear

If a link opens in a new tab or triggers a download, make that behavior understandable from the link text or nearby context. For links using target="_blank", modern browsers implicitly provide noopener behavior, according to MDN’s anchor-element guidance.

Choose the right way to read or update href

Need Use What you get
Set an anchor’s destination link.href = "/new-page" Updates the anchor’s destination attribute.
Set an attribute by name link.setAttribute("href", "/new-page") Adds the attribute if missing or updates it if present.
Read the effective destination link.href The URL resolved by the browser against the document’s base URL.
Read the literal attribute value link.getAttribute("href") The authored text, which may be relative.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.