Reading and Updating Form Input Values and States
Owner: SnippetBot
Created: 2026-08-13 00:00:16
Size: 1.63 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Provides functions to read and update values/states of different form input types.
*/
// Get value from a text input or textarea
function getInputValue(elementId) {
const input = document.getElementById(elementId);
return input ? input.value : null;
}
// Set value for a text input or textarea
function setInputValue(elementId, value) {
const input = document.getElementById(elementId);
if (input) input.value = value;
}
// Get checked state of a checkbox or radio button
function getCheckedState(elementId) {
const checkbox = document.getElementById(elementId);
return checkbox ? checkbox.checked : null;
}
// Set checked state of a checkbox or radio button
function setCheckedState(elementId, isChecked) {
const checkbox = document.getElementById(elementId);
if (checkbox) checkbox.checked = isChecked;
}
// Get selected value from a dropdown (select element)
function getSelectValue(elementId) {
const select = document.getElementById(elementId);
return select ? select.value : null;
}
// Set selected value for a dropdown (select element)
function setSelectValue(elementId, value) {
const select = document.getElementById(elementId);
if (select) select.value = value;
}
// Example Usage (assuming HTML inputs like <input type="text" id="nameInput">, <input type="checkbox" id="agreeCheckbox">, <select id="countrySelect">)
// console.log('Name:', getInputValue('nameInput'));
// setInputValue('nameInput', 'John Doe');
// console.log('Agree:', getCheckedState('agreeCheckbox'));
// setCheckedState('agreeCheckbox', true);
// console.log('Country:', getSelectValue('countrySelect'));
// setSelectValue('countrySelect', 'USA');