mardi 23 février 2021

Dropdown menu select required if checkbox is checked

I'm going to ask you a help with HTML/Javascript (I must not use Jquery).

I have a document with lots of checkboxes. Usually I check one or more checkboxes and I click the button Save to pass to another page. Only if I check the checkbox with the id "cool" it has to be mandatory to select one choice from the dropdown menu with id "list". So if I select the checkbox "cool" and I don't select a choice from the dropmenu it must not be possible to save the page. I tried several codes with javascript (script one and script two) but they don't work. Can you help me?

This is the checkbox

<input type="checkbox" id="cool" name="Performance" value="ON">

This is the dropdown menu

<select id="list" name="Info">
         <option></option>
         <option>Cat</option>
         <option>Dog</option>
         <option>Bird</option>
        </select>

Script One I tried

<script>
   function scriptone()
     {
       var checkBox=document.getElementById("cool");
       if (checkBox.checked == true)
          {     document.getElementById("list").required =  true;
          } 
      }
</SCRIPT>

Script Two I tried

<script>
        document.getElementById("cool").addEventListener('change', function(){
        document.getElementById("list").required =  this.checked;
 }  
</SCRIPT>

Let me know if you need further information. Thank you.




Aucun commentaire:

Enregistrer un commentaire