Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
How can I download text from an HTML text area as a .txt file using JS?

I'm working on a project that randomly generates a set of characters intended to be used for a password and want the user to be able to download the results as a text file. I've read some things on BLOBs and a few other methods that haven't worked. It'd be a big help if someone could offer a method that works.

TL;DR: Need a way to download text from an HTML textarea as a .txt file using JS

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should be able to programmatically create a link that uses a data url with your text, add a download attribute so the browser downloads instead of navigating (and to specify a filename), and click it.

This doesn't run as a SO snippet, presumably for security reasons, but it should work out in the world (and you can copy/paste this in your dev tools console to test it):

const generatedText = 'some generated blob of text';

// create an anchor element that we can programmatically click
const a = document.createElement('a');

// set up a data uri with the text
a.href = `data:text/plain,${generatedText}`;

// set the download attribute so it downloads and uses this as a filename
a.download = 'the-name-of-the-file.txt';

// stick it in the document
document.body.appendChild(a);

// click it
a.click();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!