2

I have stopped a link / anchor tag trigger event when it clicked by using the preventDefault() and in the middle of the jQuery function, I want to trigger the link by disabling the preventDefault(). Below you can see my code block,

jQuery(".tcf_add_to_cart a").on('click', function( e ){
e.preventDefault();
if( some condition )
{
    $.ajax({
        type : "POST",
        data : data,
        url : 'url',
        success : function( response ) {
            if ( response == 'true' )
            {
                // ENABLE THE LINK AND TRIGGER LINK ACTION                         
            }
        }
    });
}
});

as mentioned above I need to enable the blocked link and what is the best & right wayto do this.

mapmalith
  • 1,303
  • 21
  • 38

2 Answers2

3

You can just set the location based on the event target.

if ( response == 'true' ) {
  window.location.href = e.target.href
}

You can not cancel the preventDefault since the call is asynchronous.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • instead of `e.target.href` I assigned the value to a varible, `var to_page = $(this).attr('href');` and then used it like `window.location.href = to_page;` – mapmalith Oct 25 '17 at 08:39
1

Create a hidden link and when you get to the point where you want it to navigate, use that hidden link with a click event...

<a href="http://www.yourLink.com">click me</a><a href="http://www.yourLink.com" id="yourLink">&nbsp;</a>

and then in your code add:

$("#yourLink").click();

to trigger it.

also, you could hide the link like: $("#yourLink").hide();

DIEGO CARRASCAL
  • 1,999
  • 14
  • 16