HTML DOM Nav Object

HTMLWeb DevelopmentFront End Technology

The HTML DOM Nav Object in HTML represents a <nav> element.

Let us see an example of HTML DOM Nav object −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Nav</title>
<style>
   * {
      padding: 2px;
      margin:5px;
   }
      form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML-DOM-Nav</legend>
         <div id="navbar"></div>
         <input type="text" id="textSelect" placeholder="link name...">
         <input type="button" value="Create Nav Bar" onclick="createNAV()">
         <div id="divDisplay"></div>
      </fieldset>
   </form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var textSelect = document.getElementById("textSelect");
   var navDiv = document.getElementById("navbar");
   function createNAV() {
      if(textSelect.value !== ''){
         textSelect.disabled = true;
         var navObject = document.createElement("NAV");
         navDiv.appendChild(navObject);
         var aElement = document.createElement("A");
         aElement.setAttribute("href", "#");
         var textNode = document.createTextNode(textSelect.value);
         aElement.appendChild(textNode);
         navObject.appendChild(aElement);
      }
   else
   divDisplay.textContent = 'Please Provide a link name';
   }
</script>
</body>
</html>

Output

Clicking ‘Create Nav Bar’ button with empty input −

Clicking ‘Create Nav Bar’ button with non - empty input −

raja
Published on 25-Oct-2019 07:34:16
Advertisements