lundi 10 octobre 2016

Show result from checkbox with JavaScript

I wanna show my result from a checkbox with javascript I need it to send it to the database with php

I have column for each checkbox

if the chekbox is checked I take the variables and insert them into my database

else I Leave the column empty

the html code and javascript

var checks = document.getElementsByClassName('justtest');
          var str = '';
          for( i=0; i<3; i++){
              if(checks[i].checked === true){
                str += checks[i].value + " ";
              }

            }
alert(str);
<div class="form-group">
   <div class="col-md-3">
      <div class="checkbox">
         <label>
<input type="checkbox" value="test1" class="justtest" > Test1
         </label>
       </div>
      </div>
     </div>
    <div class="form-group">
    <div class="col-md-3">
     <div class="checkbox">
       <label>
 <input type="checkbox" value="test2" class="justtest"> test2
       </label>
      </div>
     </div>
    </div>
     <div class="form-group">
      <div class="col-md-3">
       <div class="checkbox">
         <label>
<input type="checkbox" value="test3" class="justtest"> test3
          </label>
         </div>
       </div>
      </div>

I try to do this but without any result

var checks = document.getElementsByClassName('justtest');
          var str = checks.length;
          for( i=0; i<3; i++){
              if(checks[i].checked === true){
                str[i]= checks[i].value;
              }

            }
alert(str[0]);
alert(str[1]);
alert(str[2]);



Aucun commentaire:

Enregistrer un commentaire