Javascript Reference - HTML DOM Nav Object








The Nav object represents an HTML <nav> element.

Standard Properties and Events

The Nav object supports the standard properties and events.

Example

We can access a <nav> element by using getElementById().


<!DOCTYPE html>
<html>
<body>
<nav id="myNav"><a href="#">HTML</a> |<a href="#">CSS</a></nav>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!--   w  w w .jav  a 2  s  . c om-->
<script>
function myFunction() {
    var x = document.getElementById("myNav").innerHTML;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

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


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="myP"></p>
<script>
function myFunction() {<!--   w  w  w.  j a  v  a 2 s  .  c o m-->
    var x = document.createElement("NAV");   
    document.body.appendChild(x);

    var anchorElmnt = document.createElement("A");
    anchorElmnt .setAttribute("href", "http://example.com");
    var txt1 = document.createTextNode("HTML");
    anchorElmnt .appendChild(txt1);

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

</body>
</html>

The code above is rendered as follows: