Javascript DOM HTML Anchor text Property get

Introduction

The text property sets or gets the text content of a link.

Return the text content of a link:

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

Click the button to display the text content of the link.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p><a id="myAnchor" href="http://www.java2s.com">Example link</a></p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*ww  w . java 2 s . c  om*/
  var x = document.getElementById("myAnchor").text;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related