Frage

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?

War es hilfreich?

Lösung

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');

Andere Tipps

You can use .trigger.

Note: It will not trigger the default action of the anchor tag.

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

Demo: Fiddle

Just something to keep in mind here, remember to give your href link an ID and rather trigger the click event on that, otherwise, if you have any other links on that page, that function will be triggered on all of them.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top