How do I get the value of an element in JavaScript?

Here are different ways based on the type of element you are trying to get value using javascript:

For Input Elements (text, password, etc.)

HTML
<input type="text" id="myInput" />
JavaScript
var inputValue = document.getElementById("myInput").value;

For Select Elements

HTML
<select id="mySelect">
   <option value="option1">Option 1</option>
   <option value="option2">Option 2</option>
</select>
JavaScript
var selectedValue = document.getElementById("mySelect").value;

For Textarea Elements

HTML
<textarea id="myTextarea"></textarea>
JavaScript
var textareaValue = document.getElementById("myTextarea").value;

Leave a Reply

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