Javascript DOM HTML document anchors Collection get by index

Introduction

Get the HTML content of the first <a> element in the document:

var x = document.anchors[0].innerHTML;

Click the button to display the HTML content of the first anchor in the document.

View in separate window

<!DOCTYPE html>
<html>
<body>

<a name="html">HTML Tutorial</a><br>
<a name="css">CSS Tutorial</a><br>
<a name="xml">XML Tutorial</a><br>


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

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

<script>
function myFunction() {//from   www . j a  v a2  s.com
  var x = document.anchors[0].innerHTML;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related