How do I get the value of text input field

Asked by: joseph-brandt
Date:
Viewed: 156
Answers: 1
  • 0

Hi,

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

Thanks

Answers

Answer by: jenniryan

Answered on: 26 Jan 2023 Marked as best by question author

  • 1

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>

 

Please log in to post an answer!