Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a click on a newly added dom element

Im trying to call a click on this newly created anchor:

$('.file-status').html("Finished downloading <a class='download' download href='#{fileEntry.toURL()}'>#{name}</a>")
$('.download').click()  

But the click event is not called. Not sure why this is?

Im trying to force a download, not open the link in the browser. Like Dropbox does

EDIT

Here is the code, so its more clear:

fileEntry.createWriter ((fileWriter) ->
    fileWriter.onwriteend = (e) ->
        $('.file-status').html("Finished downloading <a class='download' download href='#{fileEntry.toURL()}'>#{name}</a>")
        $('.download').trigger('click');
    fileWriter.onerror = (e) ->
        console.log "Write failed: " + e.toString()
    fileWriter.write blob

), errorHandler

UPDATE:

So after understanding from the answers below that this is not really possible, except if the server sends the data to me with the header Content-disposition: attachment. But this seems to me like a realy bad solutions to pure HTML5 JS apps, that might be offline.

So I found this, that handles this super awesomely! It works great. Here is the link:

http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side

Hope this helps someone that wants to do the same as I am sure there are many!

like image 316
Harry Avatar asked Jan 30 '26 01:01

Harry


2 Answers

Try binding the click event to the anchor tag and call click event.

$(".download").bind("click", function (e) {
            var $a = $(this);

                    window.location = $a.attr("href");

            });

 $(".download").click();
like image 176
Monie corleone Avatar answered Jan 31 '26 19:01

Monie corleone


If I understood you correctly, you want to simulate a user click ? If that's the case, this is how you do it:

$('.file-status').html("Finished downloading <a class='download' download href='#{fileEntry.toURL()}'>#{name}</a>");

// simulate/trigger a click
$('.download').trigger('click');
like image 36
Dimitar Dimitrov Avatar answered Jan 31 '26 19:01

Dimitar Dimitrov