Get href Value of an Anchor Tag with JavaScript | Plantpot

Get href Value of an Anchor Tag with JavaScript

Web Dev
Sponsored links

In this article, I will introduce 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;
Sponsored links

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>