JSFiddle - React, Tailwind, and code Playground
by TechplexEngineer
HTML
<input type="text" class="filename" value="file.txt"/>
<br/>
<textarea class="fileguts">Some text to download</textarea>
<br/>
<button class="download">Download Text</button>
JavaScript
function download (text, filename) {
if (typeof filename === "undefined") {
filename = false;
};
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
if (filename) {
pom.setAttribute('download', filename);
}
pom.click();
};
$(document).ready(function() {
$('.download').click(function(e) {
var filename = $('.filename').val();
var fileguts = $('.fileguts').val();
download(fileguts, filename);
});
});