Data attributes and contenteditable
HTML can carry small custom data values and allow editable content, but these features need clear limits.
✓ What you will learn in this topic
- Explain the purpose of Data attributes and contenteditable in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
A data-* attribute stores a custom string on an element for scripts or styling hooks. It is not secret storage: users can inspect and edit it. Contenteditable lets a user modify an elementâs contents in the browser, but that change is not automatically sent to a server or saved. Editors need a clear purpose, focus behavior, validation, and an explicit save or cancel path.
Syntax
Use data-name=\"value\" for a small custom string; apply contenteditable only when an editing interaction is intentionally designed.
Example 1: a small page
The data attribute provides a non-sensitive category value to a script.
<button type=\"button\" data-topic=\"botany\">Show botany resources</button>Expected result: A normal button appears; the data value is available to client-side code but is not shown automatically.
Example 2: a practical page
The category is a script hook and the paragraph can be edited locally, but saving it requires separate application behavior.
<article data-course-level=\"beginner\">
<h2>Seed starting</h2>
<p contenteditable=\"true\">Add your own study note here.</p>
</article>Expected result: The browser lets a user edit the note in place; reloading discards the edit unless code saves it.
Common mistakes
Do not treat data attributes as a secure store or assume contenteditable automatically saves edits.
Best practices
Use native disclosure and form controls first; keep data attributes small, non-sensitive, and documented for their script consumer.
Practice exercise
Create an FAQ with details and summary, add a small data attribute for a non-sensitive category, and describe how JavaScript might enhance it without removing the native behavior.
Lesson summary
- Custom data is inspectable and editable; contenteditable changes are local until an application saves them.
- Use native disclosure and form controls first; keep data attributes small, non-sensitive, and documented for their script consumer.
Knowledge check
Does contenteditable automatically persist a userâs changes?
Show the explanation
No. It changes the current document in the browser; application code must store the result.
Practical example
html<button type=\"button\" data-topic=\"botany\">Show botany resources</button>
Expected result:
A normal button appears; the data value is available to client-side code but is not shown automatically.
Hands-on practice task
Create an FAQ with details and summary, add a small data attribute for a non-sensitive category, and describe how JavaScript might enhance it without removing the native behavior.
In this section
Other lessons in HTML APIs Overview
Broaden your knowledge