
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element contentEditable Property
The HTML DOM element contentEditable property is used to get (retrieve) and set (update) the editability state of an element's content.
This property allows you to make the text content inside a particular element editable for users. It is similar to boolean types: true indicates that it is editable, and false indicates that it is not editable.
Note: If the value is set to inherit, it means that the element can inherit editability from its parent element.
Syntax
Following is the syntax of the HTML DOM Element contentEditable (to set the property) −
element.contentEditable = value
Here, the value is the new value that you want to assign to this property, it can be either 'true' or 'false'.
Use the following syntax to get the contentEditable property −
element.contentEditable
Parameters
Since this is a property, it will not accept any parameter.
Return Value
his property returns a string 'true' for editable content and 'false' for non-editable content.
Example 1: Setting Content Editable Paragraph
The following program demonstrates how to use the HTML DOM Element contentEditable property to make the content of a paragraph (<p>) element editable −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element contentEditable</title> <style> #editable { border: 1px solid #ccc; padding: 10px; width: 300px; } </style> </head> <body> <h3>HTML DOM Element contentEditable Property</h3> <p>Content Editable Paragraph</p> <p id="editable"> Click here to edit this paragraph. You can modify the text directly.</p> <p><strong>Note:</strong>Click inside the paragraph above to start editing.</p> <script> let content = document.getElementById('editable'); content.contentEditable = true; </script> </body> </html>
The program sets the contentEditable property for a paragraph element, making it editable.
Example 2: Retrieving the ContentEditable Property Value
Here is another example of the HTML DOM Element contentEditable property. This property is used to retrieve the value of the contentEditable attribute −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element contentEditable</title> <style> #editable { border: 1px solid #ccc; padding: 10px; width: 300px; } </style> </head> <body> <h3>HTML DOM Element contentEditable Property</h3> <p>Click the below button to retrieve contentEditable property value</p> <p id="editable" contenteditable="false">This is a not editable content</p> <button onclick="display()">Display contentEditable Property</button> <p id="res"></p> <script> function display(){ let content = document.getElementById('editable'); document.getElementById('res').innerHTML = "The contentEditable property value: " + content.contentEditable; } </script> </body> </html>
When the button is clicked, it displays the contentEditable property value as "false".
Example 3: Checks the editability of a Div Content
In the example below, we use the contentEditable property to check whether the content of the <div> element is editable or not −
<!DOCTYPE html> <html lang="en"> <head> <style> .editable { border: 1px solid black; padding: 10px; } </style> </head> <body> <h3>HTML DOM Element ContentEditable Property</h3> <p>Check editability with a click!</p> <div class="editable" contenteditable="true" id="editablePara"> Edit me! Am I editable? Can you modify me? </div> <br> <button onclick="checkEditable()">Check Editable</button> <script> function checkEditable() { const editablePara = document.getElementById('editablePara'); const isEditable =editablePara.isContentEditable; alert(`Is Editable: ${isEditable ? 'Yes' : 'No'}`); } </script> </body> </html>
When the button is clicked, an alert message is displayed indicating whether the content of the div is editable ("Yes") or not ("No").
Example 4: Switch between Editable and Non-editable Content
This example allows us to control the editability of the content by using a toggle button, which enables and disables the editability of the content on the web page −
<!DOCTYPE html> <html lang="en"> <head> <style> .editable { border: 1px solid black; padding: 10px; margin-bottom: 20px; } .edit-mode { background-color: #94b0ff; } .btn { cursor: pointer; } .btn:hover { background-color: #45a049; } </style> </head> <body> <h3>HTML DOM Element ContentEditable Property</h3> <p>Control the editability of the below content!!</p> <div class="editable" id="editablePara" contenteditable="true"> <p>This paragraph is editable.Click the button below to toggle edit mode.</p> </div> <button class="btn" onclick="toggleEditable()">Toggle Edit Mode</button> <script> function toggleEditable() { const editablePara = document.getElementById('editablePara'); editablePara.contentEditable = editablePara.contentEditable === 'true' ? 'false' : 'true'; const btnText = editablePara.contentEditable === 'true' ? 'Disable Edit Mode' :'Enable Edit Mode'; document.querySelector('.btn').textContent = btnText; editablePara.classList.toggle('edit-mode'); } </script> </body> </html>
When the button is clicked, it toggles the content between editable and non-editable states.
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
contentEditable | Yes | Yes | Yes | Yes | Yes |