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

0 votes

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.

Aug 2, 2022 in HTML by Ashwini
• 5,430 points
3,126 views

1 answer to this question.

0 votes

For the button you can do

<form method="get" action="file.doc">
   <button type="submit">Download!</button>
</form>
answered Aug 4, 2022 by Deepak
• 980 points

Related Questions In HTML

0 votes
0 answers

How do I link a JavaScript file to a HTML file?

How should a JavaScript file be linked ...READ MORE

Jul 21, 2022 in HTML by Ashwini
• 5,430 points
466 views
0 votes
0 answers

How to rename HTML "browse" button of an input type=file?

How can the browse button be changed ...READ MORE

Jul 27, 2022 in HTML by Tejashwini
• 3,820 points
1,493 views
0 votes
0 answers

How to disabling and enabling a html input button?

let's say I have a button like  <input id="button" ...READ MORE

Jul 5, 2022 in HTML by Tejashwini
• 3,820 points
392 views
0 votes
0 answers

How do I create an HTML button that acts like a link?

How can I make a button in ...READ MORE

Jul 8, 2022 in HTML by Tejashwini
• 3,820 points
393 views
0 votes
1 answer

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

You can trigger a download with the ...READ MORE

answered Nov 7, 2022 in Java by Damonlang
• 700 points
2,686 views
0 votes
1 answer

How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

answered Jul 1, 2019 in Others by sunshine
• 1,300 points
23,086 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,520 points
955 views
0 votes
1 answer

How to add/update an attribute to an HTML element using JavaScript?

You can read here about the behaviour of attributes ...READ MORE

answered Aug 4, 2022 in HTML by Deepak
• 980 points
879 views
0 votes
1 answer

How to set a border for an HTML div tag?

 you can use border-width:2px; border-style:solid; border-color:black; or as shorthand border: 2px solid ...READ MORE

answered Aug 4, 2022 in HTML by Deepak
• 980 points
780 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP