Javascript DOM HTML Nav Object create

Introduction

We can create a <nav> element via the document.createElement() method:

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

Click the button to create a NAV element containing a link.

View in separate window

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

<p id="myP"></p>

<script>
function myFunction() {//from  w  w  w .j av a  2 s  .  c  o  m
  var x = document.createElement("NAV");
  document.body.appendChild(x);

  var anchorElmnt = document.createElement("A");
  anchorElmnt .setAttribute("href", "/html");
  var txt1 = document.createTextNode("HTML");
  anchorElmnt .appendChild(txt1);

  x.appendChild(anchorElmnt);
}
</script>

</body>
</html>



PreviousNext

Related