Edit Web Content with HTML5 Contenteditable Attribute

By Ronald Smith

Imagine being able to edit web content directly from your browser, without any complicated software or technical know-how. It sounds amazing, doesn’t it? Well, with HTML5’s native front-end editor, that dream becomes a reality.

Content Management Systems are notorious for their complex back-end editing processes. But with HTML5’s contenteditable attribute, this process becomes a whole lot simpler. No more need for JavaScript or AJAX – now you can edit web content directly in your browser, hassle-free.

What does it do?

With the contenteditable attribute applied to any element, you have the power to make changes in an instant. It’s like having a magic wand for your web content. Look at the example below to see how it works:

Can you believe it? Now you can edit this text! It’s as easy as making a cookie muffin croissant or a faworki danish biscuit. The power is in your hands!

Let your imagination run wild as you transform this text into something completely unique. Add your own flavor and style, just like a sweet roll tiramisu or a chocolate bar sugar plum caramels. The possibilities are endless!

But let’s not get ahead of ourselves. Let me explain how this works. You see, we’ve added a special attribute called contenteditable to this text. By setting it to true, you can now edit the content directly. It’s like having your own personal word processor!

Of course, if you’re not in the mood for editing, you can simply set the value to false. That way, the content will remain uneditable, like a perfectly baked chocolate cake or a fluffy cotton candy icing.

But wait, there’s more! If the parent element of this text is also editable, you can set the value to inherit. That means the content will be editable as well, just like a delicious applicake sesame snaps liquorice pastry croissant.

So go ahead, give it a try! Be the master chef of this text and let your creativity shine. Don’t forget to click the “View Demo” button below to see the magic in action. Happy editing!

  • View Demo

How to Save Your Edits

I want to show you how you can save the changes you make. Usually, changes are stored in a database. However, for this tutorial, I’ll demonstrate how to save the changes using localStorage instead. To simplify the process, we’ll also incorporate some jQuery. If you want more information on this topic, I suggest checking out “The Past, Present & Future of Local Storage for Web Applications.”

First, let’s add a button next to our content.

Imagine eating a delicious sweet roll that tastes like tiramisu, with a chocolate bar, sugar plum caramels, and tootsie rolls. Picture a piece of chocolate cake topped with cotton candy icing. Visualize biting into an applicake, sesame snaps, liquorice pastry, croissant, caramels, fruitcake, gingerbread, and a biscuit. Now imagine enjoying a donut with toffee and candy canes.

Save Your Edits

Alright, folks, here’s the plan: when you click the button, we want to save the changes. So, let’s make that happen with some nifty scripting.

First things first, we need to set up the event. Here’s the code for that:

“`javascript

var theContent = $(‘#content2’);

$(‘#save’).on(‘click’, function() {

var editedContent = theContent.html();

localStorage.newContent = editedContent;

});

“`

This code creates a new key in localStorage to store the most recent change made to the content. Now, to check if the data has been stored successfully, we can use either Firebug or Developer Tools. Just remember to hit that button!

If you’re a Firefox user, head over to the DOM panel and search for localStorage. Chrome and Safari users can find it under the ‘Resources’ tab. Easy peasy!

Edit Web Content with HTML5 Contenteditable Attribute

So here’s what we do: we can grab that data to update the content. This is how we do it:

if(localStorage.getItem(‘newContent’))

What this bit of code does is it looks for the item named newContent in the localStorage. If it finds it, it takes the value and passes it to the selected element, which in this case is #content2. So, when we refresh the page, we should still see the change we made.

One last thing to consider

Back in the day, adding the front-end editor feature that we just showed you was a laborious and time-consuming process. It could take hours, even weeks. But now, with the contenteditable attribute, we can do it in just a second.