Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery simulate click on html anchor tag

Tags:

jquery

click

I've been trying to simulate click on a tag and this is not working as i need it to. my anchor tag looks like this

<a href="/dl/pic.jpg" target="_blank" download="file.jpg"> Download this pic </a>

a normral $("a").click() or trigger('click') wont work here.Any ideas? EDIT I need to trigger a click on this Anchor tag and causing the file to download, i dont need to add another onClick function.

like image 962
Ori Refael Avatar asked Dec 29 '25 03:12

Ori Refael


2 Answers

Using native js click event works:

http://jsfiddle.net/n6FKg/

$('a').get(0).click();
like image 117
A. Wolff Avatar answered Dec 30 '25 17:12

A. Wolff


You can achieve this using JavaScript itself by adding an id attribute

<a id="download" href="/dl/pic.jpg" download="file.jpg"> Download this pic </a>

document.getElementById("download").click();
like image 28
Deepu Madhusoodanan Avatar answered Dec 30 '25 15:12

Deepu Madhusoodanan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!