Java script:
var x = 'demo'
I want a html button from which we can download x as anyname.txt in which the value should be demo
Java script:
var x = 'demo'
I want a html button from which we can download x as anyname.txt in which the value should be demo
In order to download a file when clicked on a button, the file has to exist within our project files (next to your HTML file...). So you're going to have to:
<a>
tag because in order to download it we have to reference the file using href
attribute.download
attribute. That attribute will download the file with the name that it has. If you want the file to be downloaded as anyname.txt you just have to add a value that you want to the download attribute -> download="Anything you want"
The final code should look like this:
<a href="yourfile.txt" download="Anything you want">Download</a>
Of course, style it in CSS because this looks nothing like a button.
When clicking on this button locally (before publishing you site) it will not download the file because the computer already knows you have it. That's normal. Once published, when someone (even you) click on the button it will download the file as intended.