Create a Nav Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Nav

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create a NAV element containing a link</button>

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

<script>
function myFunction() {//w  w  w  . ja  v  a 2  s. c  om
    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>

Related Tutorials