Skip to main content

How to Get the Value of an Input in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<input>.value
<input>.value = <text>;
PartWhat it does
<input>A form control: an <input>, a <textarea> or a <select>.
<input>.valueReads 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:

index.html, a page with a label Your name, a text box with the id name, a Say hello button and an empty paragraph, and a script with a sayHello function that reads the text box's value and writes Hello, name! into the paragraph, run when the button is clicked.

index.html
<!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 page in Chromium: Your name with Ana in the text box, a Say hello button, and below them Hello, Ana!

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • document.getElementById('name').value was Ana, 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​

MistakeWhat happensDo this instead
Reading the value once, when the page loadsIt's '' then, because nobody has typed yet: the greeting said Hello, ! although Ana was typedRead value inside the function that runs on the click
Doing arithmetic with a number field's valueFor <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 selectedA radio button's value is its value attribute, 'yes', even when it isn't selectedRead checked
Using textContent on an inputIt's empty: an input's text lives in valueUse value

Troubleshooting​

SymptomLikely causeFix
The value is always emptyIt was read before anything was typedRead it in the click or submit handler
1 + 1 gives 11The values are stringsConvert with Number first
Cannot read properties of null (reading 'value')The input wasn't foundCheck 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​