0

Given the following anchor:

<a class="example" href="record1234">link</a>

How can the actual value of href be retrieved with vanilla Javascript?

const link = document.querySelector('.example');

console.log(link.href);
// https://exampledomain.com/current/document/path/record1234

console.log(link.pathname)
// /current/document/path/record1234

// with jQuery
$('.example').attr('href');

// what I want to do:
console.log(link.href.string); // record1234
nmax
  • 981
  • 1
  • 11
  • 19

1 Answers1

2

Use .getAttribute on the element.

const link = document.querySelector('.example');
console.log(link.getAttribute('href'));
<a class="example" href="record1234">link</a>
Chase
  • 5,315
  • 2
  • 15
  • 41