How to Get the Value of Text Input Field using JavaScript?

To show the value of an input field in a <div> element using JavaScript, you can use an event listener to detect changes in the input field and then update the content of the <div>.

<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Display Input Value</title>
      <style>
         #display {
         margin-top: 20px;
         padding: 10px;
         border: 1px solid #ddd;
         }
      </style>
   </head>
   <body>
      <label for="inputField">Enter something:</label>
      <input type="text" id="inputField">
      <div id="display">Your input will appear here.</div>
      <script>
         // Get references to the input field and the display div
         const inputField = document.getElementById('inputField');
         const display = document.getElementById('display');
         
         // Add an event listener to the input field
         inputField.addEventListener('input', function() {
             // Update the display div with the current value of the input field
             display.textContent = inputField.value;
         });
      </script>
   </body>
</html>

administrator

Leave a Reply

Your email address will not be published. Required fields are marked *