Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML button to save div content using javascript

I need to save content of div using pure javascript. I just edited one fiddle but I can't make it works :(

jsfiddle

<div id="content">
<h1>Hello world</h1>
<i>Hi everybody</i>

Download

function download(){
    var a = document.body.appendChild(
        document.createElement("a")
    );
    a.download = "export.html";
    a.href = "data:text/html," + document.getElementById("content");
}
like image 934
nosmoke Avatar asked Jan 08 '23 13:01

nosmoke


1 Answers

Close, you need innerHTML & trigger the click too:

function download(){
    var a = document.body.appendChild(
        document.createElement("a")
    );
    a.download = "export.html";
    a.href = "data:text/html," + document.getElementById("content").innerHTML; // Grab the HTML
    a.click(); // Trigger a click on the element
}

Working Fiddle

like image 117
CodingIntrigue Avatar answered Jan 11 '23 01:01

CodingIntrigue