Using HTML contenteditable attribute

Posted on Jul 31, 2021

Learn how you can use the contenteditable attribute to edit any HTML element content

Photo from Unsplash

The HTML contenteditable attribute is used to make your HTML tags content editable. Setting the attribute allows you to click on any HTML content rendered on the browser and edit it.

For example, here’s how to make <h1> and <p> tags content editables:

  <h1 contenteditable="true">Hello World!</h1>
  <p contenteditable="true">This is an opening paragraph</p>

When you open the HTML page on the browser, you can click on the <h1> content and change it:

The changes that I made above produces the following HTML <body> content:

  <h1 contenteditable="true">
    Hello World!
    <div>Edited by Nathan</div>
  <p contenteditable="true">
    This is an opening paragraph.
    <div>Now it has two lines.</div>

The contenteditable attribute can be inserted into any valid HTML element, even the <body> tag.

Now although you can add the attribute to the <head> tag, you can’t edit the content of the <head> tag because it’s not visible on the browser. To edit the content, you need to be able to click it.

Also, keep in mind that the edited content is not saved inside the value attribute like in <input> tags.

The value of your contenteditable element needs to be retrieved by using either innerHTML or innerText property.

When you retrieve the content using innerHTML, the new HTML tags generated from your changes will be preserved:

// "Hello World!<div>Edited by Nathan</div>"

When you use the innerText property, the tags will be replaced with the keystroke value.

The <div> tag above will be replaced with a newline \n character:

// "Hello World!\nEdited by Nathan"

The contenteditable attribute accepts either true or false as its value.

The attribute value is true by default, so you can write the attribute without assigning any value to make it editable:

  <h1 contenteditable>Hello World!</h1>
  <p contenteditable>This is an opening paragraph</p>

Both elements above will be editable when you open the page from the browser.

Finally, you can also style the editable text bold or italic by using the CTRL+B or CTRL+I shortcut on Windows and CMD+B or CMD+I shortcut on MacOS.

Listening to contenteditable element change event

If you need to listen for a change event when the user clicks and edit your contenteditable element, you can use the JavaScript addEventListener() method and listen to the input event.

The following shows how to listen to changes in the <h1> tag content:

  <h1 contenteditable id="editableHeader">Hello World!</h1>
  <p contenteditable>This is an opening paragraph</p>
    let header = document.getElementById("editableHeader");
    header.addEventListener("input", function(event){
      console.log("Header content changed");

Now each time the content of the <h1> tag above changed, the innerHTML value of the element will be logged to the browser console.

And that’s how the contenteditable attribute works 😉

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.