Get href Value of an Anchor Tag with JavaScript

02/03/2022

Contents

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>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Get href Value of an Anchor Tag with JavaScript</title>
</head>
<body>
  < a href="https://plantpot.works/" id="target">Plantpot
  <script language="javascript" type="text/javascript">
  const a = document.getElementById('target');
  console.log(a.href);
  //https://plantpot.works/
  </script>
</body>
</html>