You can make the text inside any HTML element editable by adding the contenteditable
attribute.
<div contenteditable>
Hey, I'm like a textarea kinda now!
</div>
I wouldn’t say there are wheelbarrows full of use-cases for that, but it’s neat. One possible use might be an in-progress design in which editing the content from the design itself is useful either for you, or for someone else (a client?) who needs to change the text.
So, great, contenteditable
. Now someone can click into the text and edit it.
There is nothing permanent about those changes. Refresh the page, look in another browser or whatever. Those edits are gone.
Say you wanted to do a little better job and make the changes persistent. You aren’t trying to build a CMS here, or save the data through an authenticated connection to a database or anything. You just wanna make the edits to the text are maintained if the page refreshes.
One way is to chuck the data from the text changes you make into localStorage
.
- When text is edited (on
blur
of the element), save the data tolocalStorage
using a namespace and the
ID of the element as the key. - When the page loads, look through
localStorage
and see if there are any keys that match elements on the page and, if so, replace the content.
const editables = document.querySelectorAll("[contenteditable]");
// save edits
editables.forEach(el => {
el.addEventListener("blur", () => {
localStorage.setItem("dataStorage-" + el.id, el.innerHTML);
})
});
// once on load
for (var key in localStorage) {
if (key.includes("dataStorage-")) {
const id = key.replace("dataStorage-","");
document.querySelector("#" + id).innerHTML = localStorage.getItem(key);
}
}
See the Pen
localStorage + contenteditable by Chris Coyier (@chriscoyier)
on CodePen.
This reminds me of a few other things…
document.designMode = "on"
is like a shortcut for making every element on the page behave like it hascontenteditable
.- Mavo is a little bit like this concept only with authentication, true data storage options, and editing UI.
- If performance is a concern, KV storage is supposed to be a speeder async version of
localStorage
.
Would it be possible to save this data some where more permanent?
What do you mean?
Sure. Instead of saving it in localStorage you can either save in a .db file or datbase. Or in a session. Depends on your purpose
Yes, but that’s what a CMS (like WordPress) is for. This article is about being able to have a web scratch pad more than a CMS. If you could work with a client and make dynamic changes like this you could get things fine(r) tuned and then incorporate those changes more permanently for a follow up review session.
TIL
document.designMode
.You can make it more permanent with ajax and send it to a database.
in React, it needs change to “contentEditable”, and it will give you a warning
You can also show the head and style/script elements with
display: block
and set them contenteditable to change css or javascript on the fly.I actually love using this for prototypes and POCs that are passed off to devs. Helps make a more realistic demo for clients and stakeholders.
Is there any way to make the changes visible to others? (I am working with html only)