In this article, I will show you how to download div content using javascript on button click. To save the content of the div you need to trigger the click event and get downloaded from the innerHTML using elementId.
<script type="text/javascript">
function download() {
var a = document.body.appendChild(
document.createElement("a")
);
a.download = "newfile.html";
a.href = "data:text/html," + document.getElementById("content").innerHTML;
a.click(); //Trigger a click on the element
}
</script>
<div style="border: 1px solid #357ae8; width: 500px; height: 350px">
<div id="content">
<h1 style="color: #3d75bd;">Dear user download me....</h1>
<i>click below download button</i>
</div>
<button onclick="download()">Download</button>
</div>
Output:
Post your comments / questions
Recent Article
- How to add two numbers in Android Studio? | Source Code
- FindViewByID returns null in android studio -SOLVED
- Saving changes is not permitted in SQL SERVER - [SOLVED]
- Restore of database failed. File cannot be restored over the existing. -[SOLVED]
- One or more projects in the solution were not loaded correctly in Visual Studio 2019 | FIXED
- How to find Laptop's Battery Health?
- SOLVED-Related Field got invalid lookup: icontains error in Django
- How to enable Search Feature in Django admin?
Related Article