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



Edit in JSFiddle

 
To get the text of an anchor element, use the .text() method.

JS


HTML



Edit in JSFiddle

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



Edit in JSFiddle

 
This can also be done using the getAttribute() method, which gets the value of the href attribute on the specified anchor tag.

JS


HTML



Edit in JSFiddle

That’s all about getting the href value of an anchor tag in JavaScript and jQuery.