<a download="monty.txt" href="data:text/plain;charset=utf-8,My name is Monty.">Download Text File with Name</a>
<a class="dynamic">Download Multi-line Text File</a>
xxxxxxxxxx
body {
margin: 20px auto;
font-family: 'Lato';
font-weight: 300;
}
a {
display: block;
margin: 2rem;
font-size: 2rem;
font-weight: bold;
}
xxxxxxxxxx
const link = document.querySelector('a.dynamic');
let name = 'Monty';
let text = `My name in ${name}.
I love writing tutorials.`;
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
link.setAttribute('download', `${name.toLowerCase()}.txt`);
This Pen doesn't use any external JavaScript resources.