Asked • 05/14/19

How to trigger a file download when clicking an HTML button or JavaScript?

This is crazy but I don't know how to do this, and because of how common the words are, it's hard to find what I need on search engines. I'm thinking this should be an easy one to answer.I want a simple file download, that would do the same as this: <a href="file.doc">Download!</a>But I want to use an HTML button, e.g. either of these: <input type="button" value="Download!"> <button>Download!</button>Likewise, is it possible to trigger a simple download via JavaScript? $("#fileRequest").click(function(){ /* code to download? */ });I'm definitely *not* looking for a way to create an anchor that looks like a button, use any back-end scripts, or mess with server headers or mime types.

1 Expert Answer

By:

Ian M. answered • 05/16/19

Tutor
5.0 (2,509)

Professional Web Designer, Developer, UI/UX

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.