Question

I want to override javascript confirm() with jquery UI custom dialog so i can use it like this:

<form action="http://google.com">
   <input onClick="return confirm('go Google?')" id="nupp" value="nupp"/>
</form>

I have already overrode it but the thing is that i cant get it functioning like original one. I know i cant make it 100% work like original, but whats the closest thing i can make? They say that i might be able to do it with deferred objects or with callback.

Right now i have managed to make it functioning only with submit button and one form on page. If i got two forms or something else it wont work. I might be able to make it recognize buttons and what form goes with what submit button. Code is here:

window.confirm = function(c) {
$(this).submit(function(e){
    e.preventDefault()
});
    $('#overrideAlert').text(c).dialog({
        resizable:false,
        autoOpen:true,
        modal:true,
        dialogClass: "alert",
        title:'Teade',
        draggable:false,
        closeOnEscape:false,
        buttons: {
            "Ok" : {
                id: "okay",
                text: "Ok",
                click: function(){

                }
            },
            "Cancle" : {
                id: "cancle",
                text: "Cancle",
                click: function(){
                    $(this).dialog('close');    
                }
            }
        }
    });

};

But now im thinking that i must to something like this (it is broken, just trying to look into it):

window.confirm = function(c) {
    var dfd = $.Deferred();
        $('#overrideAlert').text(c).dialog({
            resizable:false,
            autoOpen:true,
            modal:true,
            dialogClass: "alert",
            title:'Teade',
            draggable:false,
            closeOnEscape:false,
            buttons: {
                "Ok" : {
                    id: "okay",
                    text: "Ok",
                    click: function(d){
                        $(this).dialog('close');
                        var a = 1;
                        console.log(a);
                        dfd.resolve();
                    }
                },
                "Cancle" : {
                    id: "cancle",
                    text: "Cancle",
                    click: function(){
                        $(this).dialog('close');
                        var a = 0;
                        console.log(a);
                        dfd.reject();
                    }
                }
            }
        });
        console.log(dfd);
        return dfd.promise();

};
$.when( confirm('hey') ).then(
        function() {
            console.log("true")
        },
        function(d) {
            console.log("false")
        });

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top