0

I try to implement a function which allows to save a file of data previously computed by a JavaScript code.

HTML snippet code:

<button id="btn-save" type="submit" >Save to file</button>

JavaScript snippet code:

$("#btn-save").click( function() {

  for (var i=0; i< indexArray; i+=1)
  {
   content += arrayLatitude[i]+" "+arrayLongitude[i];
   content += "\n";
  }

  // Build a data URI
  uri = "data:application/octet-stream," + encodeURIComponent(content);
  location.href=uri;
});

The problem is that I have seen a lot of solutions involving the creation of "a" tag by doing :

title = document.title+"_"+day+".html",
a = document.createElement("a");
a.setAttribute("href", uri);
a.setAttribute("download", title);
document.body.appendChild(a);

But I don't know how to adapt it for "button" tag and not "a" tag.

What is the best way to accomplish directly this functionality with "button" tag ?

halfer
  • 19,824
  • 17
  • 99
  • 186

0 Answers0