I want to save working hours in two option, first form:input or second radiobutton “24/7”. While choose “24/7” radiobutton cannot deselect button.
Here is my form in .jsp file:
<div class="margin-vertical-10 col-xs-12">
<div class="form-centered">
<div class="label-float overme-ovr">
<label for="deviceWorkingHours" class="overme llabel">Рабочие часы</label>
</div>
<form:input disabled="${disable}" path="deviceWorkingHours"
id="deviceWorkingHours" type="text"
placeholder="08:00 - 23:00"
class="form-control" style="width: 25%"/>
<div class="radio-custom radio-primary">
<form:radiobutton disabled="${disable}"
id="workingHours247"
path="deviceWorkingHours"
name="workingHours247" value="24/7"/>
<label for="deviceWorkingHours" class="cursor-default">24/7</label>
</div>
<div class="form-centered">
<form:errors path="deviceWorkingHours" style="color:red;"/>
</div>
</div>
</div>
here is my js code:
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const radio = document.getElementById('workingHours247');
const textInput = document.getElementById('deviceWorkingHours');
toggleWorkingHoursInput(radio);
radio.addEventListener('change', function() {
toggleWorkingHoursInput(this);
});
textInput.addEventListener('focus', function() {
const radio = document.getElementById('workingHours247');
radio.checked = false;
toggleWorkingHoursInput(radio);
});
});
function toggleWorkingHoursInput(radio) {
const textInput = document.getElementById('deviceWorkingHours');
if (radio.checked) {
textInput.disabled = true;
textInput.value = '24/7';
} else {
textInput.disabled = false;
textInput.value = '';
}
}
</script>
What I tried:
I tried everything but radiobutton dont want to deselect)
My expectation is:
When click to 24/7 radiobutton it should deselect