How to Check If Input Box is Empty in Javascript

 

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <div>
        <input type="text">
        <button>Check</button>
        <h1>Result</h1>
    </div>
    <script src="script.js"></script>
</body>
</html>

 

CSS:

body {
    text-align: center;
}

input, button {
    display: inline-block;
    padding: 10px 20px;
}

div {
    display: inline-block;
}

 

Javascript:

let btnCheck = document.querySelector('button');
let input = document.querySelector('input');
let result = document.querySelector('h1');

btnCheck.addEventListener('click', () => {
    let value = input.value.trim();

    result.innerText = value.length == 0 ? 'Empty' : 'Not Empty';
});

 

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •