Assuming there is an unknown list of input types in a form, so it can be a mix of many (radios, selects, texts etc..), and when the page refreshes I get a key-value object from the backend that represents the input names and their values, is it possible to set their values easily without knowing the type, or there are cases where just knowing the input name and value is not enough?
For example, the form:
<form id="myform">
<input type="text" name="text1" />
<input type="radio" name="radio1" />
<input type="radio" name="radio2" />
<select name="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
Then the data object from the backend:
{
"text1": "foo",
"radio1": "bar",
"radio2": "hello",
"select1": "world"
}
Then this is the script I made
function setInputs(inputs) {
let inputElement;
for (const [key, value] of Object.entries(inputs)) {
inputElement = document.querySelector(`#myform [name='${key}']`);
inputElement.value = value;
}
}
But will it set all the inputs that are possible to set? Of course some inputs aren't possible, like type="file", but will my setInputs() function set what's possible? Or I need to cover more use cases and what I did is not enough?
type="email",type="select"and if it has amultipleattribute