Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
   <title>Demonstrate ID exists tag checking</title>
</head>
<body>
  
  <!-- tag doesnt exist yet. Click button it creates a div.
  second time you click the button console message displayed. -->
  
  
  <button onclick="addDiv()">Add a div with id='tag exist'</button>
  
  <script>
  
  function addDiv() {
     
    if(document.getElementById("tag_exist")) {
      // do nothing div already exists  
      console.log("Div with id='tag_exists' already");
    } else {
      // append the div to body
      var div = document.createElement("div");
      div.setAttribute("id", "tag_exist")
      div.innerHTML = "I now exist";
      document.body.appendChild(div);
    }
  }
    
  </script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
oreillyrosspro
0viewers