How to get the input's value with a button

By FoxLearn 12/18/2024 8:21:22 AM   109
To get the value of an input using a button, you can use JavaScript to retrieve the input's value when the button is clicked.

Here's a simple example:

<input type="text" id="myInput">
<button onclick="getValue()">Get Value</button>

<script>
function getValue() {
  // Get the input element
  var input = document.getElementById("myInput");
  // Get the value of the input
  var value = input.value;
  // Display the value
  console.log(value);
}
</script>

In this example, there is an input field with the id "myInput" and a button with an onClick event handler that calls the getValue() function. Inside the function, getElementById() is used to get the input element by its id, and the value property is used to retrieve the input's value, which is stored in a variable called value. The value can then be displayed using console.log