samedi 10 juillet 2021

How to Loop Through Rows of Checkboxes in an HTML Table in Javascript

I have a question about how to loop through checkboxes in a table. What I want to do is create two checkboxes per row in a table along with a pulldown menu. I will run a conditional in javascript based upon if both checkboxes per row are selected. If both checkboxes in a row are selected along with a pulldown menu option, I will run a conditional. So I want to loop through each row of the table checking if both checkboxes are selected. I think another way to do this is to maybe use divs and classes. How do I do this?

My sample code below is not in a table, but it is what I have so far just to get the checkboxes and pulldown menu to work.

<html>
<div class="container">
<input type="checkbox" class="checks" value ="Apple">Apple<br>
<input type="checkbox" class="checks" value ="BananaValue">Banana<br>
<input type="checkbox" class="checks" value ="Carrot">Carrot<br>



<form>
  Select your favorite fruit:
  <select id="mySelect" /*onchange="run();"*/>
    <option value="apple">apple</option>
    <option value="orange">Orange</option>
    <option value="pineapple">Pineapple</option>
    <option value="banana">Banana</option>
  </select>
</form>

<input type="submit" onclick="run()" />

<script>




function run(){


var checks = document.getElementsByClassName('checks');
var str = '';

for (i = 0;i < 3; i++){


      if(checks[i].checked === true){
         str += checks[i].value + " ";
     
}     
   }


     alert(str);



}
</script>
</div>
</html>



Aucun commentaire:

Enregistrer un commentaire