HOWTO · JavaScript
How to Change Input Value in JavaScript
Select an HTML input and assign to its value property to change its current value in JavaScript.
On this page
To change what an HTML text input currently contains, select the intended element and assign a string to its value property. This updates the current value shown by the control. It is different from changing the value content attribute, which represents the control’s default value.
Change the Current Input Value with the value Property
For example, this markup gives the input an ID and an initial value:
<input type="text" id="user-name" value="Some Value">
In a browser page, select that element with document.getElementById("user-name"), then assign the new text with input.value = "New value". The assignment changes the current value; it does not rewrite the original HTML attribute. The same property is used when a person types into the input.
Here is a self-contained JavaScript example of the assignment. It uses a small object to keep the property operation runnable outside a browser; in page code, input is the selected input element instead.
const input = { value: "Some Value" };
input.value = "New value";
console.log(input.value);
The standalone JavaScript example prints:
New value
Select the Input Element
Use getElementById() when the input has a unique ID. It returns the element with that ID, or null if none exists, so check the result before assigning when the element may not be present. For example, page code can use const input = document.getElementById("user-name"); followed by if (input) input.value = "New value";.
Use querySelector() when a CSS selector expresses how to find the input, such as document.querySelector('input[name="userName"]'). It returns the first matching element, or null if there is no match. Make the selector specific enough to identify the intended control, especially when a page has several inputs. The selector examples here are browser-page snippets, not standalone executable programs.
A form can also be reached through the document.forms collection. For a form named profile with a control named userName, page code can access document.forms["profile"].elements["userName"]. This depends on those names being present and is useful when working with a known form; an ID or CSS selector is often clearer for general element lookup. document.forms is a collection, not a function.
Understand setAttribute() and the Default Value
An input’s value property represents its current value. The value content attribute supplies the default value, exposed through the defaultValue property. Calling setAttribute("value", "New default") changes that attribute and the default. It is not a general substitute for assigning to input.value when the goal is to change what the user currently sees. In particular, after the current value has been changed through the property or by user editing, changing the default does not mean the current value should be treated as updated.
Use input.value = "New value" for the immediate current value. Use input.setAttribute("value", "New default") when the markup-level default is what you intend to change. These operations have related names but different targets; choose based on whether the task concerns the current control state or its default.
Verify the Result and Choose an Approach
After selecting the input, read input.value to inspect its current value. If the value did not change, first confirm that the selector matched the intended element and that the assignment ran after the input was available in the page. A missing result from getElementById() or querySelector() is null; attempting to assign through it will fail, so guard the lookup. Also confirm that the code is assigning to the property rather than changing only the default attribute.
For a direct update, select by a unique ID with getElementById() or by a suitable CSS selector with querySelector(), then assign to value. Use document.forms when form and control names are known. Reach for setAttribute() only when changing the default/content attribute is the intended result. The distinction between the current value and the default explains why an attribute change may not update the displayed text as expected.