jeudi 2 juin 2016

showing Confirm message when unchecking a checkbox in javascript. If "cancel", remain checked

I want a message that reads "are you sure?" to display when the user tries to uncheck a checkbox. If they choose something like "yes", then uncheck it. If they choose "no", then leave as is. I'm fairly new to JavaScript and my attempt at this has prompted me with the error "JavaScript runtime error: Unable to get property 'checked' of undefined or null reference". Here is my Checkbox code:

<div id="ChkBox">
    <input style="margin: 5px; " type="checkbox" name="chkIsActive" onchange="Areyousure('ChkBox')" value="@Model.Pharmacy.IsActive" @(Model.Pharmacy.IsActive =="True" ? "checked=\"checked\"" : "") /> Is Active
</div>
<script>
    Areyousure();
</script>

and here is the function:

function Areyousure(id) {
    if (document.getElementById(id).checked == true) {
        return false;
    } else {
        var box = confirm("Are you sure you want to Uncheck this?");
        if (box == true)
            return true;
        else
            document.getElementById(id).checked = true;
    }
}

What can i do to fix this? Thanks in Advance!




Aucun commentaire:

Enregistrer un commentaire