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:
<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
- 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:
Rank #2
const link = document.querySelector("#help-link");
if (link) {
link.href = "/support";
}
You can also update the attribute with setAttribute():
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minutelink.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().
Rank #3
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
- 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
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.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →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.
Quick Recap
Best Value
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.




