Skip to content
Tutorials Center
My learning

Topics

Lesson 79 of 122 ยท Interactive Elements

Editable Content: contenteditable, spellcheck and inputmode

Make any element editable with contenteditable, control spell checking, and pick the right mobile keyboard with inputmode, enterkeyhint and autocapitalize.

Most of the time, text on a web page is read-only and the only places visitors can type are form controls such as <input> and <textarea>. But HTML also lets you turn any element into an editing surface with the contenteditable attribute. Rich-text editors, note-taking apps and "click to edit" labels are all built on it.

A second group of attributes tunes the typing experience itself: spellcheck controls red underlines, and inputmode, enterkeyhint and autocapitalize tell phones and tablets which on-screen keyboard to show. This lesson covers all of them, building on the global attributes every element accepts.

Making an element editable

Add contenteditable to an element and the visitor can click into it and type, delete and select text, just like in a word processor. The attribute takes these values:

Value Meaning
true (or an empty attribute) The content is editable, including formatting that the browser's shortcuts produce.
false Not editable. Useful to lock a part inside an editable region.
plaintext-only Editable, but only as plain text: pasting formatted text drops the formatting, and shortcuts like bold do nothing.

If the attribute is missing, the element inherits editability from its parent, so you can put it on a wrapper and every descendant becomes editable.

HTML
<article contenteditable="true">
  <h2>Edit me</h2>
  <p>Click anywhere in this article and start typing.</p>
  <p contenteditable="false">This paragraph is locked.</p>
</article>

Notice that the element gets keyboard focus without any extra work. In practice, give it a visible focus style (browsers add an outline by default; do not remove it without a replacement), as covered in Keyboard Navigation and Focus.

A click-to-edit note

The input event fires after every change, so you can react as the user types, exactly as with a form field. There is also a beforeinput event, which fires before the change and can be cancelled with preventDefault(). That is how editors block unwanted edits, such as line breaks in a single-line title.

Reading and saving the content

JavaScript sees an editable element as a normal element, so you read its content with these properties:

  • element.textContent or element.innerText for the plain text.
  • element.innerHTML for the text with any markup the user created.
  • element.isContentEditable to check whether editing is currently enabled.

Be careful with innerHTML. If you ever put user-provided HTML back into a page (yours or somebody else's), an attacker can sneak in scripts or event handlers. Prefer plain text with textContent, or run HTML through a sanitizer before showing it to other people. The plaintext-only value helps here, because users cannot create markup in the first place.

Switching editing on and off

Since contenteditable is an ordinary attribute, you can toggle it from JavaScript, which is perfect for an "Edit" and "Save" button pair.

Edit and save mode

When not to use contenteditable

contenteditable is powerful but easy to get wrong:

  • It does not submit with forms. Content inside an editable <div> is not sent when a <form> is submitted. For data that must go to a server, use <input> or <textarea> (see Textarea and Buttons) or copy the text into a hidden field with JavaScript.
  • No built-in accessibility semantics. A plain div does not announce itself as a text field. If you really need an editable region as a custom control, give it role="textbox" (and aria-multiline="true" when it has several lines) and a label, as described in ARIA.
  • Browsers differ. The markup produced when users press Enter or paste content varies, and cleaning it up is a large part of the work in rich-text editors. Mature libraries exist for this; building a full editor from scratch is rarely a good idea.
  • The old formatting API is deprecated. document.execCommand() (used for bold, italic and so on) still works in browsers but is marked obsolete and has no replacement of equal power. Fine for experiments, risky for products.

For a short single-line value, an <input> is nearly always the better choice. Use contenteditable when you need editable text with structure, such as paragraphs, lists or inline formatting.

Spell checking

Browsers underline misspelled words in editable text. The spellcheck attribute lets you turn this behavior on or off for an element and its descendants:

HTML
<textarea spellcheck="true"></textarea>
<input type="text" name="username" spellcheck="false">
<div contenteditable="true" spellcheck="false">const code = 42;</div>

The values are true, false and the empty string (same as true). When the attribute is absent, the browser decides, usually checking multi-line fields and skipping some single-line ones.

Turn it off wherever red squiggles would be noise: usernames, email addresses, product codes, code snippets and names of people. Leave it on for prose such as messages and comments. Remember that the browser's dictionary follows the page language, so set lang on the <html> element and on foreign-language passages, as explained in Global Attributes.

Two related text-entry attributes exist for browsers that support them: autocorrect="off" (Safari) stops automatic corrections while typing, and the writingsuggestions attribute can switch off AI-style suggestions in browsers that offer them. Both are optional extras; simply treat them as hints.

Choosing the mobile keyboard

On phones, the on-screen keyboard changes with the field. You already know that type="email" or type="tel" select a suitable layout (see Input Types). But those types also add validation and change how values are stored, which is sometimes unwanted. For example, a credit card number or an order code is a string of digits that you do not want treated as a number, and type="number" shows spinner buttons and drops leading zeros.

The inputmode attribute fixes this. It affects only the keyboard, not validation or the value:

inputmode Keyboard
text Default keyboard for the language.
numeric Digits 0 to 9 only. Ideal for PINs, codes, ZIP codes.
decimal Digits plus a decimal separator for the user's locale.
tel Telephone pad with digits, +, * and #.
email Letters with @ and . close at hand.
url Letters with / and .com style keys.
search Layout tuned for search, often with a Search key.
none No virtual keyboard at all, for pages that draw their own.

A typical use is a one-time code field:

HTML
<label for="otp">6-digit code</label>
<input id="otp" name="otp" type="text" inputmode="numeric"
       autocomplete="one-time-code" pattern="[0-9]{6}" maxlength="6">

It stays a text input, so leading zeros survive, but phones show the number pad. Here autocomplete="one-time-code" lets a phone offer a code from an SMS, and pattern keeps the validation you learned in Form Validation.

The Enter key and capitalization

Two more attributes polish the keyboard:

  • enterkeyhint changes the label or icon of the Enter key. Values: enter, done, go, next, previous, search and send. A search box can show "Search", a chat box can show "Send", and fields in a long form can show "Next".
  • autocapitalize controls automatic capital letters on virtual keyboards. Values: none (also written off), sentences (also on), words and characters. Use none for usernames and email addresses, words for names and titles, and characters for codes like license plates.

The attributes do nothing on a desktop keyboard, but you can still see them on the elements. This demo lists the attributes that decide how three different fields behave:

Fields tuned for typing

Open this page on a phone to feel the difference: the ZIP field shows a digit pad, the plate field types capital letters, and the Enter key label changes between fields. Always test on a real device, because keyboards vary between Android and iOS.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which value of contenteditable allows editing but removes formatting from pasted text?

    plaintext-only lets the user type, but the element only keeps plain text. Pasted formatting and formatting shortcuts have no effect.
  2. 2. You place an editable div inside a <form> and submit it. What is sent to the server?

    Only form controls such as input, textarea and select are submitted. You must copy the content into a control yourself.
  3. 3. A field collects a 6-digit code that can start with zero. What is the best markup?

    inputmode="numeric" shows a digit keyboard but the value stays a string, so leading zeros are kept. type="number" would treat it as a number.
  4. 4. What does autocapitalize="none" do?

    It tells virtual keyboards not to capitalize letters automatically, which is right for usernames and email addresses.

Summary

  • contenteditable turns any element into an editing area. Values are true, false and plaintext-only, and children inherit the setting.
  • Listen to input (after a change) and beforeinput (before it, cancellable); read content with textContent, and never insert untrusted HTML back into a page.
  • Editable regions are not submitted with forms and need role="textbox" and a label to be accessible; prefer <input> and <textarea> for ordinary fields.
  • spellcheck switches underlines on or off; disable it for names, codes and email addresses.
  • inputmode, enterkeyhint and autocapitalize shape the mobile keyboard without changing the field's type or validation.

Next lesson: SVG Basics, where you start drawing shapes with markup instead of images.