How do I get the value of text input field

Question

Hi,

How do I get the value of an input field using Javascript?

Thanks

Answer ( 1 )

    0
    2023-01-25T16:00:34+00:00

    To get the value of an input field in JavaScript, you can use the value property of the HTMLInputElement object.

    Here is an example:

    <input type="text" id="myInput">
    
    <script>
    // Get the input element
    var input = document.getElementById("myInput");
    
    // Get the value of the input element
    var inputValue = input.value;
    
    console.log(inputValue);
    </script>

    You can also use the value property to set the value of the input field:

    <input type="text" id="myInput">
    
    <script>
    // Get the input element
    var input = document.getElementById("myInput");
    
    // Set the value of the input element
    input.value = "Hello, World!";
    </script>

Leave an answer