How to trigger a file download when clicking an HTML button or JavaScript
For the button you can do
<form method="get" action="file.doc">
<button type="submit">Download!</button>
</form>
How to trigger a download when an HTML button is clicked
You cannot force a download using a form without doing some server-side config or using a server-side script.
However, you can use a link with the download attribute.
The css is to make it look like a button
.button-link {
font: bold 11px Arial;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #CCCCCC;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #CCCCCC;
}
<a href="file.txt" download="file.txt" class="button-link">Download</a>
How to trigger a file download with text area content when clicking an HTML button
- firstly you are selected all textarea by
SelectorAll
then generated the link without loop through it - also even if you done a loop that's will generate link for all textarea elements
- if you want to download the textarea before clicked button it's will be better to add them inside a
container
and usethis
keyword - or use
pervouisSibling
- also it's will be better to use css instead of
br
tag - you don't need jQuery because your function already uses Vanillajs
- By adding them inside container
HTML
<div class="container">
<textarea class="text_mop" style="display: none;">Welcome to 1st sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
</div>
<div class="container">
<textarea class="text_mop" style="display: none;">Welcome to 2nd sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
</div>
CSS
.text_mop, .btn_mop {
display: block;
width: auto;
}
JS
function download(file, text) {
//creating an invisible element
var element = document.createElement('a');
element.setAttribute('href', 'data:text/html;charset=utf-8, ' + encodeURIComponent(text));
console.log(element.href);
element.setAttribute('download', file);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
var btn_mop = document.getElementsByClassName("btn_mop");
for (var btn of btn_mop) {
btn.addEventListener("click", function() {
var text = this.parentNode.getElementsByClassName("text_mop")[0];
var filename = "MOP.txt";
download(filename, text.value)
});
}
- by previousSibling property
HTML
<textarea class="text_mop" style="display: none;">Welcome to 1st sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
<textarea class="text_mop" style="display: none;">Welcome to 2nd sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
CSS
.text_mop, .btn_mop {
display: block;
width: auto;
}
JS
function download(file, text) {
//creating an invisible element
var element = document.createElement('a');
element.setAttribute('href', 'data:text/html;charset=utf-8, ' + encodeURIComponent(text));
element.setAttribute('download', file);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
var btn_mop = document.getElementsByClassName("btn_mop");
for (var btn of btn_mop) {
btn.addEventListener("click", function() {
var text = this.previousSibling.previousSibling
var filename = "MOP.txt";
download(filename, text.value)
});
}
Related Topics
Encrypt With PHP, Decrypt With JavaScript (Cryptojs)
How to Send Formdata Objects With Ajax-Requests in Jquery
How to Get the Selected Radio Button'S Value
How to Change an Element'S Text Without Changing Its Child Elements
Navbar Dropdown (Collapse) Is Not Working in Bootstrap 5
Insert Ellipsis (...) into HTML Tag If Content Too Wide
(Html) Download a Pdf File Instead of Opening Them in Browser When Clicked
Why Does Forms With Single Input Field Submit Upon Pressing Enter Key in Input
How to Add/Update an Attribute to an HTML Element Using JavaScript
How to Show Progress Bar While Loading, Using Ajax
Get the Size of the Screen, Current Web Page and Browser Window
Clearing ≪Input Type='File' /≫ Using Jquery
Is There a Spec That the Id of Elements Should Be Made Global Variable
Best Way to Track Onchange As-You-Type in Input Type="Text"
JavaScript: How to Find Out If the User Browser Is Chrome
How to Stop a Web Page from Scrolling to the Top When a Link Is Clicked That Triggers JavaScript