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

128
Views
How to download an html webpage containing a filled form as an HTML file using javascript

I have a webpage that contains a form. The user will fill the fields form.

How can I save the webpage as an HTML file containing the filled fields?

The code below downloads the webpage as an HTML file but the fields are empty.

<a onClick="this.href='data:text/html;charset=UTF-8,'+encodeURIComponent(document.documentElement.outerHTML)"
   href="#" download="page.html">Download</a>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Please check the working codepen.io as the download feature does not work in Stackoverflow's snippet frame.

I just filled the "value" attribute for each input with its own value, then trigger the download.

function download(){
  var hiddenElement = document.createElement('a');
  var inputs = document.getElementsByTagName('input')
  for (var i = 0; i < inputs.length; i++)
    inputs[i].setAttribute("value", inputs[i].value);
  hiddenElement.href = 'data:attachment/text,' + encodeURI(document.getElementById("container").innerHTML);
  hiddenElement.target = '_blank';
  hiddenElement.download = 'myFile.html';
  hiddenElement.click();
}
<div id="container">
<input value="" type="text">
<input value="" type="text">
<input value="" type="text">
<button onclick="download()">Download</button>
</div>

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!