Javascript DOM HTML Anchor Object create

Introduction

The Anchor object represents an HTML <a> element.

We can create an <a> element by using the document.createElement() method:

var x = document.createElement("A");

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>Click the button to create an A element with a link to java2s.com.</p>

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

<script>
function myFunction() {//from w  w  w  .j a  v  a  2s.c om
  var x = document.createElement("A");
  var t = document.createTextNode("Tutorials");
  x.setAttribute("href", "https://www.java2s.com");
  x.appendChild(t);
  document.body.appendChild(x);
}
</script>
</body>
</html>



PreviousNext

Related