javascript message when you distich a checkbox

1

Good, I'm starting to use jquery so I still do not know much, I'd like to do the following. I have a checkbox. simple. but that I am loading by default 'checked' and I want that when trying to deface it I get a confirmation message in javascript. How could I solve this?

<div class="switch">
  <label>Guardar<input type="checkbox" value="1" class="check" checked><span class="lever"></span>
  </label>
</div>
    
asked by max 03.02.2017 в 20:48
source

1 answer

3

You could solve it like this:

  • Subscribe to the event change of checkbox .
  • If you are not checked , then ask for confirmation.
  • Depending on the response obtained, leave checked or not, checkbox .

document.getElementById('checkbox').addEventListener('change', function() {
  // Si no esta checked
  if (!this.checked) {
    this.checked = !confirm('¿Esta seguro que no quiere guardar?');
  }
});
<label><input id="checkbox" type="checkbox" checked />Guardar!</label>
    
answered by 03.02.2017 / 21:04
source