HTMLInputElement
Input Element
Represents an <input> tag. The most versatile form element — can be text, checkbox, file picker, color picker, and more depending on type.
<input
type="text"
placeholder="Enter name…"
>
Key Properties — specific to <input>
| Property | Type | Description |
|---|---|---|
| value | string | What the user has typed (or the current value). Most used property. |
| type | string | "text", "checkbox", "radio", "file", "number", "email", "password", etc. |
| checked | boolean | For checkboxes and radio buttons — true if selected. |
| placeholder | string | Hint text shown when the field is empty. |
| disabled | boolean | Prevents interaction with the input when true. |
| readOnly | boolean | User can see the value but can't edit it. |
| maxLength | number | Maximum number of characters allowed. |
Common Methods
| Method | Returns | Description |
|---|---|---|
| focus() | void | Moves keyboard focus to this input. |
| select() | void | Selects all text in the input. |
| addEventListener() | void | Listen for "input", "change", "focus", "keydown", etc. |
Quick Examples
JS
const input = document.querySelector('input');
// Read what user typed
console.log(input.value);
// Clear on button click
document.querySelector('button').addEventListener('click', () => {
input.value = '';
input.focus();
});
// Live character counter
input.maxLength = 100;
input.addEventListener('input', () => {
const remaining = 100 - input.value.length;
document.querySelector('#counter').textContent = remaining + ' left';
});
// Disable while loading
input.disabled = true;
fetch('/api/data').then(() => { input.disabled = false; });Related Elements
Related Properties
Related Methods