How do you get the value of an HTML text input field using JavaScript?
’
Select the input element with document.getElementById() or document.querySelector() and read its .value property inside an event listener.
Read Text Input Value on Button Click or Input Event
<input type="text" id="username" placeholder="Enter username" />
<button id="submitBtn">Submit</button>
JavaScript
const input = document.getElementById("username");
const btn = document.getElementById("submitBtn");
btn.addEventListener("click", () => {
const value = input.value.trim();
console.log("Entered value:", value);
}); '
You must log in or register to comment.
