jeudi 8 novembre 2018

Problem changing the value of a checkbox dynamaically using Javascript

I have a checkbox on a form which works fine. When the form s clicked I am trying to change the value to 1,, when not clicked the value should be zero. Am filling the field dynamically using Javascript. Th eproblem is when I log it in console I get an empty value..

Checkbox field

 <input type="checkbox" id="spouse" value="" class="spouse" onclick="checkMark()">

Javascript code

 function checkMark() {
      var checkBox = document.getElementById("spouse");

      if (checkBox.checked == true){
        checkBox.value = '1';
      } else {
        checkBox.value = 'O';
      }
    }

Log in console

var spouse = $('#spouse').val();




Aucun commentaire:

Enregistrer un commentaire