Search code examples
jquerybootstrap-4bootbox

Bootbox - Confirm Modal DIalog Box requires Callback function ... but I have one


Why does the below code return an error:

<script type="text/javascript">
    $(document).ready(function () {
        $("#chkIssueCredit").change(function () {
            if (!$(this).prop('checked')) {
                bootbox.confirm("Are you sure you do not want to issue a credit? Please confirm."), function (result) {
                    if (result) {
                        $(this).prop('checked', true);
                    }
                }
                return false;

                //if (!confirm("Are you sure you do not want to issue a credit? Please confirm.")) {
                //    $(this).prop('checked', true);
                //};
            };
        });
    });
</script>

Solution

  • You are closing the confirm method right after the first parameter (the message). Try this one here:

    <script type="text/javascript">
        $(document).ready(function () {
            $("#chkIssueCredit").change(function () {
                if (!$(this).prop('checked')) {
                    bootbox.confirm("Are you sure you do not want to issue a credit? Please confirm.", function (result) {
                        if (result) {
                            $(this).prop('checked', true);
                        }
                    });
                    return false;
    
                    //if (!confirm("Are you sure you do not want to issue a credit? Please confirm.")) {
                    //    $(this).prop('checked', true);
                    //};
                };
            });
        });
    </script>
    

    Can you see the difference?