mardi 31 janvier 2017

Assign a variable a value if a checkbox is clicked within a form javascript

I'm trying to assign a variable a value as long as certain checkboxes are clicked. The code below works for what I need, but as soon as I sit it within a form, it breaks the function.

I think I'm missing something pretty simple, but for the life of me can't find out how to select a form value - also would anyone have advice on how to scale this for use with several checkboxes?

function cb3(checkbox) {
  if (checkbox.checked) {
    alert("function called!");
    $userChoice1 = 'value1';
  } else {
    $userChoice1 = '';
  }
}
<script src="http://ift.tt/1qRgvOJ"></script>
<input type="checkbox" id="cb3test" name="cb3" value="value1" onClick="cb3(this)">Value1

Thanks in advance, applogies if this is a duplicate, although I couldn't find a question with regards to forms.




Aucun commentaire:

Enregistrer un commentaire