Check/Uncheck all checkboxes except disabled checkboxes

Asked

Viewed 141 times

2

In this script below it marks and unchecks all checkbox but I wish that when a checkbox is "checked" and "disabled" it doesn’t change anything. The problem is that I don’t know how to do it, someone would have an idea?

<input type="checkbox"                  class='marcar'  />
<input type="checkbox" checked disabled class='marcar'  />

JS:

<button class='btn btn-large' type='button' title='Todos' id='todos' onclick='marcardesmarcar();'>
       <i class='icon-large  icon-ok'>Click</i>
</button>

<script>
function marcardesmarcar() {
    $('.marcar').each(function () {
        if (this.checked) 
           $(this).attr("checked", false);
        else 
           $(this).prop("checked", true);
    });
}
</script>

1 answer

2


You can use $(this).prop('disabled') to check the status of that property. And join in your if like this:

    if (this.checked) {
        if ($(this).prop('disabled')) return;
        else this.checked = false;;
    }

jsFiddle: https://jsfiddle.net/wzeobpf2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.