How do you get the value of an HTML text input field using JavaScript? | A1Lab@FahadM to JavaScriptEnglish •
edit-2
12 days ago

How do you get the value of an HTML text input field using JavaScript?

message-square
0

’ 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);
}); '