Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to validate custom modal dialog form and if its not ok, prevent form from closing. For this purpose, I attached to X-close-dialog-button. Problem is to prevent it's default behavior.

function fValidateBeforeClose(event) {
    event.stopImmediatePropagation(); // Not working
}    
function fAddOnCloseHandler() {
    var oCloseButton = jQuery("a[id^='DlgClose']", parent.document);
    oCloseButton.on( "click", function (event) { fValidateBeforeClose(event)} ); 
}       
_spBodyOnLoadFunctionNames.push( ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js") );
share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

up vote 2 down vote accepted

Good way to remove handlers from dialog's close button is using $clearHandlers Method.

Check code below:

function fValidateBeforeClose(event) {
    var isValid = true;
    //set IsValid value
    if(isValid)
    {
       //close dialog
       SP.UI.ModalDialog.get_childDialog().close();
    }
    else
    {
      alert('Not valid');
    }
}  

function fAddOnCloseHandler() {
    var oCloseButton = $("a[id^='DlgClose']");

    $clearHandlers(oCloseButton.get(0));

    $(oCloseButton).on("click", function (event) { fValidateBeforeClose(event)} );
}   

_spBodyOnLoadFunctionNames.push( ExecuteOrDelayUntilScriptLoaded(fAddOnCloseHandler, "SP.js") );
share|improve this answer
Worked. I modify your answer, to remove unnecessary element search in DOM. – dbardakov May 22 at 11:49
Ok, I approved it. – Andrew May 22 at 12:01
add comment (requires an account with 50 reputation)

I think you should remove old event handler

 var js = oCloseButton.attr("onclick");
 oCloseButton.attr("onclick", "");//remove sharepoint click handler...
share|improve this answer
Good catch,jQuery(oCloseButton).off().find("*").off(); seems working. Though, I liked "the SharePoint way" provided by @Andrew better. – dbardakov May 22 at 11:48
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.