ComeRun
ComeRun

Reputation: 921

An issue with jQuery dialog

Say I have the following link:

<a onclick="confirmDelete()" href="delete.do&xyz">Delete user</a>

and the following is the confirmDelete function:

function confirmDelete()
{
   if(!confirm("delete user?"))
    {
     return false;
    } else 
    {
     return true;
    }
}

Basically when click on link, it checks whether the user select ok or cancel and then perform based on it.

Problem: I have got hundreds of similar links on my platform and we decide to turn all the javascript alerts to jquery dialog.

I used the following to somehow override the alert:

$(function(){

window.confirm = function(message) {
          $('#overrideAlert').text(message).dialog({
              resizable: false,
              modal: true,
              buttons: {
              "Ok": function() {
              $( this ).dialog( "close" );

              },
              Cancel: function() {
              $( this ).dialog( "close" );

              }
            }
        });
  };
});

It works fine and the alert comes up as a jquery dialog but the issue is because of the fact that it is not possible to return something from a handler like dialog, I am not sure how to tell my button in the filed, that the user selected ok or cancel.

Basically I m not sure how to link them all together.

Please help

Upvotes: 0

Views: 67

Answers (2)

rab
rab

Reputation: 4144

The issue is because of javascript asynchronous nature . As you are using jQuery . could try following method .

$('a[onclick="confirmDelete()"]')
      .attr('onclick','')
      .click( function(){

         var anch = this,
             message = "delete user?";

          $('#overrideAlert').text(message).dialog({
              resizable: false,
              modal: true,
              buttons: {
              "Ok": function() {
                   $( this ).dialog( "close" );
                   alert( "window.location=" + anch.href ); 
              },
              Cancel: function() {
              $( this ).dialog( "close" );

              }
            }
        });

        return false ;
});

I made jsfiddle http://jsfiddle.net/wB389/

Upvotes: 1

HMR
HMR

Reputation: 39270

Maybe like so:

"Ok": function() {
window.location=deleteURL;
$( this ).dialog( "close" );
},

It's probably a little more complicated that that since you use relative url's but you can get the current url using window.location.protocol + window.location.port + window.location. window.location.host + window.location.pathname

Upvotes: 0

Related Questions