DEV Community

Annie Liao
Annie Liao

Posted on

6 1

A Small, Simple Trick to Make Your Text Editable via HTML

Woah GIF

If you've been following my recent tweets, you might have noticed that I am currently coding along with Wes Bos' JavaScript30 challenge.

While a lot of the challenges served as a refresher for my JavaScript knowledge, there were times when I had to pause the tutorial and shouted internally, "Woah, why didn't I learn about this earlier?!"

I had that moment again when approaching the challenge of creating moveable text shadows.

As it turns out, there is an HTML global attribute called contenteditable that allows the user to edit targeted text on the browser.

According to the MDN documentation, contenteditable is an enumerated attribute (meaning it has a list of possible values, as opposed to a boolean value), where true or an empty string indicates that the text is editable, and false otherwise.

If the attribute is not given any value, the value is treated as an empty string, which makes the text editable, like so:

<div>
  <h1 contenteditable>🔥WOAH!</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

And just like that, you can type any text you want on your browser.

No CSS, no JavaScript, and no need for an input field. Just a plain little HTML attribute. (For security-related information, feel free to check out this Stack Overflow discussion.)

Woah GIF


Have you used contenteditable at work or for your projects? Is there any other front-end magic that you wish you found out sooner? Please share your experience in the comments, I'd love to see some practical use cases! 🙌

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay