How to Get the Value of an Input in JavaScript
input.value is the text that's in a text box right now: whatever the person typed, or the starting value from the HTML. Read it when you need it, for example when a button is clicked, and remember that it's always a string.
Syntax
<input>.value
<input>.value = <text>;
| Part | What it does |
|---|---|
<input> | A form control: an <input>, a <textarea> or a <select>. |
<input>.value | Reads the control's current value, as a string. |
<input>.value = <text> | Changes what the control shows. |
MDN's HTMLInputElement page describes value as the control's current value.
The Example
The example page has a text box for a name and a Say hello button. When the button is clicked, the script reads the text box's value and greets the person by name.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Input value</title>
</head>
<body>
<label for="name">Your name</label>
<input id="name">
<button type="button" id="hello">Say hello</button>
<p id="greeting"></p>
<script>
function sayHello() {
const name = document.getElementById('name').value;
document.getElementById('greeting').textContent = 'Hello, ' + name + '!';
}
document.getElementById('hello').addEventListener('click', sayHello);
</script>
</body>
</html>
addEventListener('click', sayHello) runs sayHello each time the button is clicked, so the value is read at that moment.
Step 2: Type a Name and Click the Button
Double-click index.html to open it in your browser. Type Ana into the text box, then click Say hello:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
document.getElementById('name').valuewasAna, the text in the box when you clicked.- The greeting used it straight away, through
textContent. - Each click reads the value again, so if you change the name and click once more, the greeting follows.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Reading the value once, when the page loads | It's '' then, because nobody has typed yet: the greeting said Hello, ! although Ana was typed | Read value inside the function that runs on the click |
| Doing arithmetic with a number field's value | For <input type="number" value="5">, value was the string '5' | Convert it: Number(input.value) |
Reading value of a radio button to see whether it's selected | A radio button's value is its value attribute, 'yes', even when it isn't selected | Read checked |
Using textContent on an input | It's empty: an input's text lives in value | Use value |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The value is always empty | It was read before anything was typed | Read it in the click or submit handler |
1 + 1 gives 11 | The values are strings | Convert with Number first |
Cannot read properties of null (reading 'value') | The input wasn't found | Check the id, and run the script after the input |
FAQ
How do I set the value from code?
Assign to it: input.value = 'Ben' puts Ben in the text box.
Does it work for a textarea?
Yes. A <textarea>'s text is in value too: for <textarea>Line one</textarea>, value was Line one.
Is the value trimmed?
No. Spaces someone types before or after the text are kept. Use input.value.trim() when you want them removed.
Conclusion
value holds what's in a form control at this moment, as a string. Read it inside the code that runs when the person is done typing, and convert it with Number before you calculate with it.
Additional Resources
- HTMLInputElement, on MDN.
- input: The HTML Input element, on MDN.
