How to Get a Value from an HTML Element Using JavaScript?

Estimated read time 1 min read

To get the value from an HTML element using JavaScript, you can use the element.value property.

For example, if you have an input field with the ID inputField, you can get its value using the following code:

<input type="text" id="inputField">

<script>
  var input = document.getElementById('inputField');
  var value = input.value;
  console.log(value);
</script>

In this example, document.getElementById('inputField') retrieves the element with the ID inputField, which is the input field. Then, the value property of the input field is stored in the value variable and logged to the console.

Similarly, you can get the value of other HTML elements, such as a textarea or a select dropdown, by using the same element.value property.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply