1

I have a Chrome extension that dynamically creates links and adds them to a webpage. The links download files. They each have a download attribute to name the files correctly (only works in Chrome, afaik - but it only needs to), as well as href for the url of the file. How can I simulate clicking one of these links?

I don't need to click the version within the document necessarily, just the link object.

Basically, how can I write, in Javascript/JQuery, code to have Chrome download a file named with download at the href location?

The JQuery click() command didn't seem to do anything, though I can't figure out why.

Wilson
  • 8,570
  • 20
  • 66
  • 101
  • Do you delegate event? How do you bind your event? – A. Wolff Apr 14 '13 at 20:53
  • possible duplicate of [Trigger click jquery not working](http://stackoverflow.com/questions/5867370/trigger-click-jquery-not-working) (solution = use the native `click` method, `$('selector')[0].click()`) – Rob W Apr 15 '13 at 07:11

2 Answers2

2

I know this thread is old, but it's the first result in search, so for others who seeks answer, here it is:

$(css_selector)[0].click();

And you're doing it $(function(){ HERE }); and some js functionality is added to css_selector on original page load, then maybe you want to delay HERE execution by setting timeout.

0

you can try simulating the click like this

$("your_selector").trigger("click");  

or like this

$("your_selector").click();  
cockypup
  • 1,013
  • 1
  • 10
  • 24
  • Neither of those seem to do anything and I can't figure out why. I checked out the selector and it is identifying the correct element, just no event is firing. – Wilson Apr 14 '13 at 20:40