To set a default value for an HTML input field, use the value attribute in HTML for initial markup defaults (or checked for checkboxes/radios), and set inputElement.value = "..." in JavaScript to update or initialize the live DOM property dynamically. Setting .value in JavaScript updates what the user sees and what form data submits, without altering the initial HTML value attribute.
1. Setting a Default Value in HTML (value Attribute)
In HTML markup, the value attribute defines the starting text or value when the input element is first created:
// Minimal HTML markup showing default value attributes on input types
const htmlDefaultExamples = `
<input type="text" id="username" name="username" value="guest_user" />
<input type="number" id="quantity" name="quantity" value="1" min="1" />
<input type="checkbox" id="newsletter" name="newsletter" checked />
`;
console.log(htmlDefaultExamples.trim());
- Text, number, and hidden inputs: Use
value="..."directly on the<input>tag to supply its initial value. - Checkboxes and radio buttons: Use the
checkedboolean attribute rather thanvalueto mark the box as checked by default. - Reset behavior: Calling
form.reset()restores inputs back to their original HTMLvalue(orchecked) attributes.
2. Setting or Updating the Default Value in JavaScript (.value Property)
In JavaScript, query the input element and assign to its .value property—ideal when loading saved preferences, API data, or dynamic defaults:
// Set default input values dynamically in JavaScript
function setDefaultInputValue(selector, defaultValue) {
const input = document.querySelector(selector);
if (!input) {
return;
}
// Only assign if empty or when explicitly initializing a default
if (input.value === "") {
input.value = String(defaultValue);
}
}
setDefaultInputValue("#city-input", "Lahore");
setDefaultInputValue("#qty-input", 1);
- Assigning
input.value: Directly updates the live control value in the DOM and reflects immediately in form submissions. - Guard against overwriting user edits: Checking
input.value === ""(or running before user interaction) avoids clobbering text a user already typed.
3. Common Traps to Avoid
Working with HTML value attributes and JavaScript .value properties has a few subtle differences:
- Confusing
input.valuewithsetAttribute("value", ...):input.valuechanges the live current value (anddefaultValueproperty maps to the initial HTML attribute), whereassetAttribute("value", ...)only updates the attribute and will not overwrite user edits once the user types into the field. - Assigning
nullorundefinedtoinput.value: In some browsers, settinginput.value = undefinedornullcan coerce to"undefined"or"null"or clear inconsistently—always pass a string or fallback likevalue ?? "". - Setting
.valuebefore the DOM exists: Run your JavaScript after DOM elements load (such as inside aDOMContentLoadedlistener or a module/deferred script) sodocument.querySelector(...)is notnull.
TL;DR: Use the value attribute in HTML for initial markup defaults (and checked for checkboxes/radios), and assign input.value = ... in JavaScript to set or update the live value dynamically.
Are you setting a default for a text/number input, a checkbox/radio/select control, or prefilling data from an API or localStorage?
