js_input_examples
JavaScript and HTML Input Element Examples
Section titled “JavaScript and HTML Input Element Examples”This section outlines common ways JavaScript is used to interact with HTML input elements and forms, enabling dynamic and responsive user interfaces.
Button (<button>, <input type="button">) Element
Section titled “Button (<button>, <input type="button">) Element”Manipulating button elements:
- Enable or disable a button:
buttonElement.disabled = true/false;. - Get the
nameattribute of a button:buttonElement.name. - Determine the
typeof a button (e.g., ‘button’, ‘submit’, ‘reset’):buttonElement.type. - Get or set the
valueattribute (often relevant for input type buttons):buttonElement.value. - Get or set the text displayed on a button:
buttonElement.textContentorbuttonElement.innerHTML. - Find the form a button belongs to:
buttonElement.form(returns the parent form element or null).
Event handling (e.g., buttonElement.addEventListener('click', ...) is crucial for button interactivity.
Form (<form>) Element
Section titled “Form (<form>) Element”Interacting with HTML forms:
- Programmatically submit a form:
formElement.submit(). - Reset a form to its initial values:
formElement.reset(). - Access individual form elements:
formElement.elements(an HTMLFormControlsCollection), which can be accessed by name or index (e.g.,formElement.elements['username']orformElement.elements[0]). - Get the value of each element in a form by iterating through
formElement.elements. - Find the
accept-charsetattribute of a form:formElement.acceptCharset. - Get or set the
actionattribute (URL for submission):formElement.action. - Get or set the
enctypeattribute (encoding type for submission, e.g., ‘multipart/form-data’):formElement.enctype. - Find the number of elements in a form:
formElement.elements.length. - Get or set the HTTP
methodfor sending form data (‘GET’ or ‘POST’):formElement.method. - Get the
nameattribute of a form:formElement.name. - Get or set the
targetattribute (where to display the response):formElement.target.
For modern asynchronous form submissions (e.g., using fetch or XMLHttpRequest), the FormData object is very useful: const formData = new FormData(formElement);.
Select (<select>) and Option (<option>) Elements
Section titled “Select (<select>) and Option (<option>) Elements”Working with dropdown lists and their options:
- Enable or disable a dropdown list:
selectElement.disabled = true/false;. - Get the form containing the dropdown list:
selectElement.form. - Get the number of options in the dropdown:
selectElement.options.lengthorselectElement.length. - Control display for multiple selections:
selectElement.size(number of visible options),selectElement.multiple = true/false;. - Get the selected option’s value (for single select):
selectElement.value. - Get the index of the selected option (for single select):
selectElement.selectedIndex. - Access individual options:
selectElement.options[index]. - Check if an option is selected:
optionElement.selected(boolean). - Get or set the text of an option:
optionElement.textoroptionElement.textContent. - Get or set the value of an option:
optionElement.value. - Dynamically add options:
const newOption = new Option('Text', 'value'); selectElement.add(newOption);. - Remove options:
selectElement.remove(index);orselectElement.options[index] = null;.
For multi-select lists, you’ll need to iterate through selectElement.options to find all selected options.
Other Input Types (<input type="text">, <input type="checkbox">, etc.)
Section titled “Other Input Types (<input type="text">, <input type="checkbox">, etc.)”Common tasks with various input fields:
- Get or set the value of a text input, password, email, etc.:
inputElement.value. - Check if a checkbox or radio button is selected:
inputElement.checked(boolean). Set totrueorfalseto change state. - Get the
name,type,idattributes. - Control read-only state:
inputElement.readOnly = true/false;. - Control required state:
inputElement.required = true/false;. - Access validation properties (e.g.,
inputElement.validity,inputElement.validationMessage) for HTML5 form validation.
Attaching event listeners for ‘input’, ‘change’, ‘focus’, ‘blur’ events is common for creating interactive forms. For more details on specific input types and their unique properties/methods, refer to MDN Web Docs.