How can I set a javascript const to a value in a form element that could be either a dropdown select or a checked radio button?
I have an html form in a django site that is used for both add and update. If the form is for a new entry, I want the field estimateType
to be radio buttons with three choices, one of which is checked
by default. If the form is for an edit to an existing entry, I want the form widget to change to a select dropdown with additional choices to pick from.
In the javascript in the onchange event, I need to know what the value of the estimateType
field is. I can get it to work for select, or radio input, but not both. For example, what I have right now is:
const estimateType = document.querySelector(input[name=estimateType]:checked).value;
That works for a radio button but not the select dropdown. How can I use the same const declaration but account for heterogeneous form input types?
You can use a comma to separate the selectors and it'll pull what it finds: Note that the JS is the identical for each snippet.
const estimateType = document.querySelector("input[name=estimateType]:checked,select[name=estimateType]").value;
console.log(estimateType)
<input type="radio" name="estimateType" value="A">A
<input type="radio" name="estimateType" value="B" checked>B
<input type="radio" name="estimateType" value="C">C
const estimateType = document.querySelector("input[name=estimateType]:checked,select[name=estimateType]").value;
console.log(estimateType)
<select name="estimateType" >
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D" selected>D</option>
<option value="E">E</option>
</select>