I'm working on a little project, and want to add a "Save" button which will when clicked convert everything which is inside of a certain div class into a downloadable .txt (or .pdf or any other document viewing file type) document.
I want to note one more thing, the content of that div isn't "static" (if I can call it that), since the user / visitor can edit it, to clarify, the user / visitor can delete the text in that div, as well as add new text to that div. I'm not sure does this make any kind of difference, but there it is just as a little note.
Is this possible?
Top comments (4)
This can be done through Blob constructor.
Read More below.
stackoverflow.com/questions/134051...
Cheers!
Great! Works fine as far as the saving goes..
Just how can I select the div class I want the button to save? I cannot figure that part out..
I have this on the button:
onclick="download('Test', 'MyTasks.txt', 'text/plain')"
And the 'Test' is the text which will appear in the text file, but how can I make it be instead of "Test" whatever is in my certain div class?
You can get contents with
developer.mozilla.org/en-US/docs/W...
You're likely looking for a solution that gets a
div
containing or adjacent to your button, or a child of an adjacent element. I'd suggest something along these lines:With the JS along these lines:
(Some alternatives notwithstanding depending on your particular use-case. e.g.
innerText
vstextContent
orparentNode
vsparentElement
, andtarget
vscurrentTarget
.)