samedi 27 janvier 2018

Checkbox checked or unchecked javascript

I am trying to check and see if the checkbox is checked or unchecked, however when I use element.value it is always returning false for whatever reason... I've tried to use onclick as well. I also printed out the value of element to the console to see if I can tell if the value changes, but couldn't really tell when the checkbox is true or not. Any help is great! Thanks in advance! It keeps going into the else block for whatever reason.

HTML

<div class="form-check">
    <input class="form-check-input" type="checkbox" value="" onchange="javascript:addFilter(event,'freshman');">
    <label class="form-check-label" for="defaultCheck1">Freshman</label>
</div>
<script>
    function addFilter(element,string) {
      let filter_array =[];
      if(element.value){
      if(string =="freshman"){
        filter_array.push(string);
        console.log("Add Freshman to Array");
      }
      if(string =="sophmore"){
        filter_array.push(string);
        console.log("Add Junior to Array");
      }
      if(string =="junior"){
        filter_array.push(string);
        console.log("Add junior to Array");
      }
      if(string =="senior"){
        filter_array.push(string);
        console.log("Add Senior from to Array");
      }

    }

else {

      if(string =="freshman"){
        filter_array.push(string);
        console.log("Remove Freshman from Array");
        console.log(element);
      }
      if(string =="sophmore"){
        filter_array.push(string);
        console.log("Remove Junior from Array");
      }
      if(string =="junior"){
        filter_array.push(string);
        console.log("Remove junior from Array");
      }
      if(string =="senior"){
        filter_array.push(string);
        console.log("Remove Senior from Array");
      }
    }
}
  </script>




Aucun commentaire:

Enregistrer un commentaire