Get href Value of an Anchor Tag with JavaScript



In this article, you will learn how to get href value of an anchor tag with JavaScript.

Anchor href Property

To get the href of an anchor tag, first get the element and then use the anchor href property for that element.

const element = document.getElementById("idName");
const hrefValue = element.href;

Sample Code

Below is a sample code to get the href value from the anchor tag.

<!DOCTYPE html>
  <meta charset="utf-8"/>
  <title>Get href Value of an Anchor Tag with JavaScript</title>
  < a href="" id="target">Plantpot
  <script language="javascript" type="text/javascript">
  const a = document.getElementById('target');