I have a simple contenteditable markup:
<div class="example" contenteditable="true">
<div class="inside">Some content</div>
</div>
When I delete the "Some content
", then class="inside"
div also gets deleted. Is there a way to prevent the inside div from being removed when contents are deleted?
For example, this is the look I am trying to make once the contents are deleted.
<div class="example" contenteditable="true">
<div class="inside"></div> <!-- The div is not deleted -->
</div>
I looked around but doesn't seem like there is a clear answer.
Any help will be much appreciated.
Thank you.
The contenteditable attribute specifies whether the content of an element is editable or not. Note: When the contenteditable attribute is not set on an element, the element will inherit it from its parent.
Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.
To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed. to add a contenteditable div. document. addEventListener("keydown", (event) => { if (event.
Just set contentEditable="false" . See this answer.
The contentEditable property sets or returns whether the content of an element is editable or not. Tip: You can also use the isContentEditable property to find out if the content of an element is editable or not.
By default, when you write inside an element that has contenteditable set to true, that element gets a border around on focus. However, you can use CSS to remove the border: Use the [ attribute] selector to select all elements that are contenteditable, and remove the border with the outline property:
The element's content is editable if its parent element is editable button.innerHTML = "Enable content of p to be editable!"; button.innerHTML = "Disable content of p to be editable!";
Tip: You can also use the isContentEditable property to find out if the content of an element is editable or not. The numbers in the table specifies the first browser version that fully supports the property. Specifies whether the content of an element should be editable. "inherit" - Default.
This might help someone
function onpaste(e: ClipboardEvent) {
e.preventDefault();
const selection = window.getSelection();
// Don't allow deleting nodes
if (selection.anchorNode.isSameNode(selection.focusNode)) {
// get text representation of clipboard
const text = e.clipboardData.getData("text/plain");
// insert text manually, but without new line characters as can't support <br/>s yet
document.execCommand("insertHTML", false, text.replace(/\n/g, ""));
}
}
function onkeydownInEditable(e: KeyboardEvent) {
if (e.key === "Enter") {
e.preventDefault();
}
if (e.key === "Backspace" || e.key === "Delete" || e.key === "Paste") {
const selection = window.getSelection();
// Don't allow deleting nodes
if (!selection.anchorNode.isSameNode(selection.focusNode))
e.preventDefault();
}
}
elementEditing.addEventListener("keydown", onkeydownInEditable);
elementEditing.addEventListener("paste", onpaste);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With