Harry
Harry

Reputation: 13329

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!

Upvotes: 3

Views: 743

Answers (6)

crmpicco
crmpicco

Reputation: 17181

Since this question is tagged with CoffeeScript here is a CoffeeScript valid answer (although still using jQuery).

itemId = $('.modal__product-info').attr('data-item-id')
$('#size-' + itemId).click()

Upvotes: 0

Antonio Papa
Antonio Papa

Reputation: 1666

Try this pluggin livequery

$('.download').livequery(function(){
    $(this).click();
});

Upvotes: 0

Jai
Jai

Reputation: 74738

You have not passed a event to the anchor, try this: http://fiddle.jshell.net/gnumA/

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

update:

$('.download').click(function () {
    window.location.href = $(this).attr('href');
}).click();

Upvotes: 1

Jashwant
Jashwant

Reputation: 29015

Show user the downloading has been finished and then redirect to downloading after 1 sec.

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

setTimeout(function () {
   window.location.href = $('.download').attr('href');  
},1000);

Upvotes: 1

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15158

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

Upvotes: 1

Monie corleone
Monie corleone

Reputation: 1618

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

Upvotes: 1

Related Questions