views:

172

answers:

2

hi

i've got a page (asp.net) where I trap the click event of a link. i then do some dirty checking and present a dialog to the user,

$(function() {
    var clickedLink;

    $('.checkdirty').click(function(event) { 
         if(isDirty == false){
              return true;
          }
          clickedLink = $(this);
          $('#dirtysave-dialog').dialog('open');
          return false;
    }); 

});

do you want to loose your changes Yes/No etc.

$('#dirtysave-dialog').dialog({ bgiframe: true, autoOpen: false, 
                                    height: 125, width: 425, modal: true,
                     title: "You have unsaved changes, do you want to continue and loose changes?!!",
                     buttons: {
                     "Yes": function() {
                         isDirty = false;
                         $(this).dialog("close"); 
                         $(clickedLink).click();
                     },
                     "No": function() { 
                         $(this).dialog("close"); 
                     }
                },
                open: function(type, data) {
                    $(this).parent().appendTo("form");
                }
        });

if they click yes i then clear the isDirty flag and call click on the link. this goes back in to the click event handler, does the check

if(isDirty == false){
     return true;
}

returns true but the event never happens....

i need to click the link again manually for it to fire.

any ideas??

A: 

you can use the trigger function, Change:

$(clickedLink).click();

to

$(clickedLink).trigger("click");

A better way would be to separate your click functionality out into a separate function and call this, however the above will work.

Alex S
They're the same thing.
Crescent Fresh
i've tried both of them, no joy... same behaviour in both cases..
mickdelaney
debugging in firebug it seems to traverse up the tree of parent elements calling click on each one. not sure why. i really thought it would be as simple as click(); return true; any ideas would be gr8 as i'm about to give up.
mickdelaney
`.click()` only fires the event handlers for the `click` event. It does not simulate a physical clink of the link.
Crescent Fresh
A: 

.click() only fires the event handlers for onclick, it doesn't actually make the default action of following the link happen. Probably the quickest method is just to do that manually:

window.location= clickedLink.href;

PS. “lose”

bobince
tried it but no joy, keeps saying href is undefined!!!
mickdelaney
OK, `clickedLink[0].href`, if you *must* put a jQuery wrapper around it...
bobince
ok it was $(clickedLink).attr("href"); i'm using this technique so thanks for the direction...
mickdelaney