jqueryevent-triggers

Trigger an inline function with jquery


I have a function written on the anchor itself like

<a href="home.jsp" onclick="return confirm('Do you want to logout?');">Log Out</a>

I have tried using another onclick even of the anchor like .

$("a").click(function(){ });

I know it will trigger another event not the one I expect to trigger. The content in the confirm box is dynamic, so I cant change the function. Is there any way that I could trigger the in line function of the anchor through jQuery?


Solution

  • To programmatically trigger the element's click event, simply use the click() method without an argument:

    $('a').click();
    

    This is a shorthand of the trigger() method and is equivalent to:

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