I want to disable a select attribute when my type = radio input is checked.
Here’s the part of the code i’m talking about:
<input id="societeInterne" name="radioSociete" type="radio" value="Interne"/><label>Interne / </label>
<input id="societeExterne" name="radioSociete" type="radio" value="Externe"/><label>Externe - </label>
<label for="PrestataireStit"> Préciser le STIT </label>
<ol>
<li style="list-style-type: disc">
<select id="selectPresta">
<option value='GROUPE presta' name='Societe presta'>presta</option>
<option value='GROUPE presta' name='Societe presta'>presta</option>
<option value='GROUPE presta' name='Societe presta'>presta</option>
</select>
</li>
</ol>
What I want is that when the input "societeExterne" isn’t selected, the select "selectPresta" be disabled. I tried with different types of function in javascript but none of them worked:
var radioExterne = document.getElementById('societeExterne');
var radioInterne = document.getElementById('societeInterne');
var selectPresta = document.getElementById('selectPresta');
if (radioExterne.checked) {
selectPresta.disabled = false;
} else {
selectPresta.disabled = true;
}
var radioExterne = document.getElementById('societeExterne');
var radioInterne = document.getElementById('societeInterne');
var selectPresta = document.getElementById('selectPresta');
radioExterne.addEventListener(
"change",
(event) => {
selectPresta.disabled = !event.target.checked;
},
false,
);
2
Answers
You can do it if you wrap your content in a
form
element, then you can listen for a change of the form and toggleselectPresta.disabled
accordingly:Add this to your script tag