Skip to content Skip to sidebar Skip to footer

How Do I Use Javascript To Change Value Of A Hidden Input Depending On Status Of A Checkbox?

I am trying to change the value of a hidden input field, depending on the value of a checkbox. I don't know much about Javascript but this is what I have so far. ) , hidden = document.getElementById('delterms'); checkbox.addEventListener('change', function() { hidden.value = (this.checked) ? 'Accepted' : ''; }, false); </script>

The idea is that the anonymous function gets run every time the user clicks the checkbox, which sets the value of the hidden field to either "Accepted" or the empty string, depending on whether or not the box is checked.

This jsFiddle shows a working example.

Solution 2:

Move the variable assignment into the function, thus:

functionterms() {
    var checked = document.getElementById('checkbox').checked;
    if (checked==false)

Solution 3:

Solution 4:

Because checkboxes are not submitted in a form.submit() when they are unchecked I use a hidden field that gets updated when the checkbox is clicked on.

<input type="hidden"id="chbx" value="" name="0_">
<input type="checkbox"id="__chbx" 
    onclick="document.getElementById('chbx').value = 
             document.getElementById('__chbx').checked;"/>

(I have omitted the jsp-code that adds the checked property to the checkbox-input if it was already checked when rendering the page.)

Solution 5:

Your variable checked is set only once, so it always equals to the inital value. Move it inside your function terms().

Post a Comment for "How Do I Use Javascript To Change Value Of A Hidden Input Depending On Status Of A Checkbox?"