Check if an element is available with if statement - Javascript Statement

Javascript examples for Statement:if

Description

Check if an element is available with if statement

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <button onclick="myFunction()">Click me</button> 
      <!-- <input type="text" id="demo"></input> --> 
      <script>
function myFunction() {//from   w w  w  .j av a 2  s.co  m
    var message = undefined;
    
    if (document.getElementById("demo"))
        message = document.getElementById("demo").value;
    if ( message ) 
        console.log(message);
    else 
        console.log("undefined");
}

      </script>  
   </body>
</html>

Related Tutorials