-1

Hello I have a page with <a> links to download pdf files. Now I have to put a button to download all files available on that page. Now how to trigger a click event on all links on button click ?

    <button class="download-all"/>
    <a class="link" href="file.html"/>
    <a class="link" href="file2.html"/>

Now on button click all the links with link class should be clicked.

3
  • 1
    Possible duplicate of How to programmatically trigger the click on a link Using jquery? Commented Nov 23, 2016 at 5:51
  • May I ask why you are trying to do this? Commented Nov 23, 2016 at 6:13
  • @Fredreik I have already said that in OP. I have links to download individual files, now i need to download them all at once. Commented Nov 23, 2016 at 6:32

2 Answers 2

0

I am not sure if this is possible using standard web technologies because HTTP does not support more than one file download at a once.

Solution:-

Open multiple amount of window to initiate the file download

Easiest way would be to serve the multiple files bundled up into a ZIP file.So onclick anyone can download the zip file.

Sign up to request clarification or add additional context in comments.

Comments

0

Your HTML is incorrect, Please modify with the below.

HTML,

<button type="button" class="button">Download All PDF</button>
<a class="link" href="file.html">PDF</a>
<a class="link" href="file2.html">PDF</a>

JS,

$('button.button').on('click', function(){
    $('a.link').click(); //fires click to all a with class "link"
});

//Anchor tag click Event
$('a.link').click(function(e){
    e.preventDefault();
    var href = $(this).attr("id");
    window.location.href = href;
});

3 Comments

Yeah its a bit incorrect. I just wrote it as sample.
@SrujanSujju The above HTML structure is the correct one.
Uncaught ReferenceError: click is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.