ggtffg
ggtffg

Reputation: 323

Stopping and firing an event in JavaScript

I'm click on a 'a' tag and the page is redirecting at some domain. Now I have written some function on the 'a' tag click which uses e.preventDefault(). I'm keeping target safe in another variable using var temp = e.target;.

But when I'm trying to fire it again it is not firing up without any js error.

Here is the code :-

   var allowedReload = false;
   var reloadTargetControl = null;

   $('a').click(function (e) {
   debugger;
   if (allowedReload == undefined || allowedReload == false) {

    reloadTargetControl = e.target;

    e.preventDefault();

    pageLeaveConfirmBox(); /*this function is creating custom confirm box with Yes/No button*/


    /******* Custom confirm box - yes button code *******/

    $("#popUpBtnYes").click(function () {
        debugger;
        allowedReload = true;
        $(reloadTargetControl).click(); /*this step is executing the same script again but nothing is happening, can any one explain it why? */
    });
} 
});

Upvotes: 0

Views: 32

Answers (2)

Mehran Hatami
Mehran Hatami

Reputation: 12961

to trigger an event manually you should use trigger function:

$(reloadTargetControl).trigger('click');

Other than this you could improve your code by changing:

if (allowedReload == undefined || allowedReload == false)

to

if (allowedReload === undefined || !allowedReload)

Upvotes: 1

Al DeLuca
Al DeLuca

Reputation: 56

In your #popUpBtnYes click function you set allowedReload to true, but where is the else portion of your conditional that would handle that true condition? You've sent a click event to the original target but not supplied any expression to handle the changed state. I think all you need is else { return true; } in order for this to work.

Upvotes: 0

Related Questions