
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM href Property
The HTML DOM href property is used to get (retrieve) or set (update) the source URL for an anchor (<a>) element in HTML.
It returns the current URL that the anchor links to and when modified, it updates the href attribute of the anchor, allowing you to change the link destination dynamically.
Syntax
Following is the syntax of the HTML DOM href (to get the href attribute) property −
anchorObject.href
To update (set) the href property, use the following syntax −
anchorObject.href = new_url
Parameters
Since it is a property, it does not accept any parameters.
Return value
This property returns the current URL of the anchor element as a string.
Example 1: Retrieving the URL of an Anchor ("a") Element
The following is the basic example of the HTML DOM href property. It returns the current URL of an anchor element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM href</title> </head> <body> <p>URL of the current link will be displayed below:-</p> <span>MyLink: </span> <a href="https://www.tutorialspoint.com/" id="my_link" target="_blank">Welcome to Tutorialspoint</a> <p>The URL of the above Link: </p><span id="result"></span> <script> let my_url = document.getElementById("my_link"); document.getElementById("result").innerHTML = my_url.href; </script> </body> </html>
Example 2: Updating the URL of an Anchor ("a") Element
Here is another example of using HTML DOM href property. We use this property to change the current URL of an anchor element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM href</title> </head> <body> <p>Click the button to update the URL of the below link:-</p> <span>MyLink: </span> <a href="https://www.tutorialspoint.com/" id="my_link" target="_blank">Click me</a> <button id="update_btn">Update URL</button> <p id="result"></p> <span id="new"></span> <script> document.getElementById("update_btn").addEventListener("click", ()=>{ let my_url = document.getElementById("my_link"); document.getElementById("result").innerHTML = "URL has been updated click the above link to verify"; my_url.href = "https://www.tutorialspoint.com/html/index.htm"; document.getElementById("new").innerHTML = "Updated URL: " + my_url.href; }); </script> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
value | Yes | Yes | Yes | Yes | Yes |