Skip to content
Fix Code Error

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

March 13, 2021 by Code Error
Posted By: Anonymous

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.

Solution

For the button you can do

<form method="get" action="file.doc">
   <button type="submit">Download!</button>
</form>
Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Form field border-radius is not working only on the…
  • What are the undocumented features and limitations…
  • How to setup ember engines?
  • Java ElasticSearch None of the configured nodes are…
  • binding backbone form view UIto model change to…
  • Onclick event not happening when slick slider is…
  • Why the value of input file missing when I input the…
  • sql query to find priority jobs
  • Search code inside a Github project
  • Webpack build error
  • Import Python Script Into Another?
  • Aurelia, navigate is not updating viewport
  • Active tab issue on page load HTML
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How can I implement Advanced Search using HTML?
  • The definitive guide to form-based website authentication
  • Best way to sort through components Vue JS
  • For-each over an array in JavaScript
  • What is the correct way to split an aurelia app into…
  • How can I find the product GUID of an installed MSI setup?
  • What does "Fatal error: Unexpectedly found nil while…
  • Collapse a vector of function calls in to one line,…
  • Getting the closest string match
  • Smart way to truncate long strings
  • Search input with an icon Bootstrap 4
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Accessing query values in Next.js
  • How to find out client ID of component for ajax…
  • Multiple word search using trie in dart
  • creating triggers for After Insert, After Update and…
  • What is an optional value in Swift?
  • python pandas - add unique Ids in column from master…
  • Accessing and filtering child components defined in…
  • How do I create a search box like on…
  • "Thinking in AngularJS" if I have a jQuery background?
  • Why does C++ code for testing the Collatz conjecture…
  • VueJS: Why Trigger 'Input' Event Within 'Input'…
  • Cannot read property '$i18n' of undefined when using…
  • Vue - Multiple radio inputs, checked value?
  • How do I compare sequential characters in two…
  • Various ways to remove local Git changes
  • What's the best way of scraping data from a website?
  • How do I limit the number of digits from 6 to 4 in…
  • What are type hints in Python 3.5?
  • Backbone Relational not setting reverse relations properly?
  • Implementing autocomplete
  • Creating a SearchView that looks like the material…
  • Best practice multi language website
  • Minimal webpack setup for Aurelia without easy-webpack
  • What does this symbol mean in JavaScript?
  • Javascript Uncaught TypeError: Cannot read property…
  • What is your most productive shortcut with Vim?
  • RegEx match open tags except XHTML self-contained tags
  • How can I manually compile a svelte component down…
  • Trying to keep dropdown menus flush to the edge of…
  • Good MapReduce examples
  • How can I add class active if the element click on…
  • Calculate sum for group of dynamic table rows in jquery
  • What is a NullReferenceException, and how do I fix it?
  • Clear and reset form input fields
  • Detect if Visual C++ Redistributable for Visual…
  • XMLHttpRequest cannot load ✘✘✘ No…
  • VueJS: Cannot read property 'name' of undefined"
  • How to implement a basic iterative pushdown…
  • NextJS - can't identify where state change is…
  • Logging best practices
  • Unable to select the LinkedIn 'locations' button…
  • React and Express iTunes Search API :: Error:…
  • React : How to change route on onChange event of input
  • Ukkonen's suffix tree algorithm in plain English
  • ReferenceError: obj is not defined using…
  • How to change the color of vaadin-select-text-field…
  • Polymer focus() has to be wrapped in setTimeout() to work?
  • Not submit any value if checkbox is empty
  • Vue: v-model and input event in custom component…
  • How can I fix MySQL error #1064?
  • Identifying and solving…
  • How to recover stashed uncommitted changes
  • data.table vs dplyr: can one do something well the…
  • How to handle Vue 2 memory usage for large data (~50…
  • 2D cross-platform game engine for Android and iOS?
  • How can I pattern match ID only making sure the…
  • Highlight a word with jQuery
  • How do search engines deal with AngularJS applications?
  • C++ template,typename and operator
  • Ember JS passing a parameter with insert-newline…
  • How to select a component and deselect the others in…
  • Angular: Can't find Promise, Map, Set and Iterator
  • Why in some case the selected of the iron-page doesn't work?
  • Clear localStorage and change the view Backbone
  • Replace every other instance of a string
  • How to auto insert the current user into my db when…
  • What is the best way to signal 50+ nested components…
  • How to get the next sibling match in a list with…
  • How to loop through all the buttons to a click event…
  • What are the nuances of scope prototypal /…
  • jQuery Mobile: document ready vs. page events
  • How to use Servlets and Ajax?
  • Backbone.js - Best Practice for Implementing…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to close current tab in a browser window?

Next Post:

How to drop rows of Pandas DataFrame whose value in a certain column is NaN

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error