Trigger ~ HTML5 Download Attribute

Asked

Viewed 293 times

0

Good morning, you guys, I have the following flaw : I need to download some text files (.txt) with the attribute download but I need to do this automatically, when I click the page or a Rigger that activates, the problem is that I don’t know how to activate the element that has the attribute. I’ve tried to

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

Thanks in advance.

  • Tries $('.meuElemento').click()

  • @Juniornunes7 I tried too, but it returns the element "<a>. refDownload"

  • Boy that strange, maybe it only works for one element and not several, tries to make a loop with all its elements and shoot the event one by one.

  • @Juniornunes7 But that’s what I want to kkkk, download the files one at a time, but it didn’t work any of the above ways.

  • Okay, here in the comment it is difficult to put code, I will post a response with the code and you see if it will work for you, if it doesn’t work I delete.

  • @Juniornunes7 All right, thank you in advance for trying to help.

Show 1 more comment

1 answer

1


You can do the following between the tags a you can put the text inside a span with an identifier (in case I put the download class) and use the code to click from one to one, the problem is that the browser asks permission to download all the files at once.

<a href="http://www.petcidade.com.br/wp-content/uploads/2016/09/cachorro-tenta-pegar-petisco-imagem-1-reproducao.jpg" download="teste"><span class="download">teste1</span></a>

<script>
    $('.download').trigger('click');
</script>

NOTE: The download attribute did not work for this link, but for a local file it worked in the test I did here!

Or you could do it that way here too:

$('a').each(function(i, el) {
    window.location = $(el).prop('href');
});

That way you wouldn’t have to tag span, but would not have control over the attribute download.

  • 1

    It worked right as I need it, thank you .

  • In fact that each is not even necessary, you can leave only the $('.download').trigger('click') that will work, I will edit!

  • Right, but I just needed to activate the element anyway, now I see how it can be better here in my case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.