Click here to Skip to main content
15,888,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a universal dialog popup function which will asked the users for confirmation before executing save or delete functionality.But the on clicking the save button the page it not wait for the popups reply.Here is my code.

What I have tried:

My function (returns true or false ):-
Java
function DiaglogNormal(_title, _message) {
    debugger;
    
    BootstrapDialog.confirm({
        title: _title,
        message: _message,
        type: BootstrapDialog.TYPE_PRIMARY,
        closable: true, 
        draggable: true,
        btnCancelLabel: 'Cancel', 
        btnOKLabel: 'Yes',
        btnOKClass: 'btn btn-success',
        btnCancelClass: 'btn btn-warning',
        callback: function (result) {
            // result will be true if button was click, while it will be false if users close the dialog directly.
            if (result) {
                //alert('Yup.');
                return true;
            } else {
                //alert('Nope.');
                return false;
            }
        }
    })
    }


Control where it is called :

HTML
<input type="submit" name="button" formnovalidate value="@WFCommon.BTN_Save"  id="btnSave" class="btn btn-primary" />


Function when the button is clicked:

JavaScript
$("#btnSave").click(function () {
            debugger;           
            
            if (DiaglogNormal('Confirmation', 'Continue To Save ??...'))
                {
                    RemoveInputReviewerRules("#WorkflowDraft", 'input[alt="txtApprovers[]"]');
                }
                else
                {
                    return false;
                }
            
        });


Please help on how to make the click event function to wait for the calling function result.
Posted
Updated 3-Jan-17 0:21am
v2
Comments
Try returning false from the click outside else.
pranav8265 3-Jan-17 9:24am    
thanks for your reply .I tried that also but before my function returned the true/false the post back event is getting fired.

1 solution

Unfortunately, BootstrapDialog.confirm() is asynchronous, so it does not wait. You form will go ahead with the submission upon clicking of the submit button. To get around this, you have to first prevent this default behavior of the submit button from happening by adding:
$("#btnSave").click(function(e){
        e.preventDefault();
        // omitted
This will stop the form from submitting and allow the confirm dialog to show.
Next, you will add code to submit the form when the return from callback is true, do this:
callback: function(result) {
    if(result) {
	// whatever code you want to run 
	// to submit the form
	$("#idOfForm").submit();
     }
}
Putting together the whole picture:
$("#btnSave").click(function(e){
        e.preventDefault();
		
        BootstrapDialog.confirm({
            // omitted
            callback: function(result) {
                if(result) {
		     // whatever code you want to run on callback
		     // to submit the form
                     $("#idOfForm").submit();
                }
            }
        });

})
 
Share this answer
 
Comments
pranav8265 3-Jan-17 9:22am    
thanks for your help it worked .Also I have changed button type as button instead of submit and now it is working fine
Peter Leow 3-Jan-17 12:43pm    
Glad that your problem is solved. Yes, using is another way. Cheer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900