Get href value of an anchor tag with JavaScript/jQuery
This post will discuss how to get the href value of an anchor tag in JavaScript and jQuery.
1. Using jQuery
In jQuery, you can use the .prop() method to get the href value of an anchor tag. This is demonstrated below:
JS
HTML
To get the text of an anchor element, use the .text() method.
JS
HTML
2. Using JavaScript
In vanilla JavaScript, you can use the querySelector() , which will return the first anchor tag within the document. Then we can directly access the href property to get the exact value of the href attribute. The following code demonstrates this.
JS
HTML
This can also be done using the getAttribute() method, which gets the value of the href attribute on the specified anchor tag.
JS
HTML
That’s all about getting the href value of an anchor tag in JavaScript and jQuery.
Average rating 4.41 /5. Vote count: 37
No votes so far! Be the first to rate this post.
We are sorry that this post was not useful for you!
Tell us how we can improve this post?
Thanks for reading.
Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and help us grow. Happy coding 🙂
This website uses cookies. By using this site, you agree to the use of cookies, our policies, copyright terms and other conditions. Read our Privacy Policy. Got it
Anchor href Property
The href property sets or returns the value of the href attribute of a link. The href attribute specifies the destination of a link.
Browser Support
Syntax
Property Values
Value | Description |
---|---|
URL | Specifies the URL of the link. |
- An absolute URL — points to another web site (like href=»http://www.example.com/default.htm»)
- A relative URL — points to a file within a web site (like href=»default.htm»)
- An anchor URL — points to an anchor within a page (like href=»#top»)
Technical Details
Return Value: | A String, representing the URL of the link. Returns the entire URL, including the protocol (like http://) |
---|
More Examples
Example
Example
Another example of how to get the URL of a link (a relative URL):
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.