Javascript DOM HTML Anchor Object get from document

Introduction

The Anchor object represents an HTML <a> element.

We can access an <a> element by using getElementById():

var x = document.getElementById("myAnchor");

Demo

Click the button to get the URL of the link.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p id="myResult"></p>
<button onclick="myFunction()">Test</button>

<a id="myAnchor" href="https://www.java2s.com">Tutorials</a>

<script>
function myFunction() {//www. j  a va2 s .  c  o  m
  var x = document.getElementById("myAnchor").href;
  document.getElementById("myResult").innerHTML = x;
}
</script>
</html>



PreviousNext

Related