Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contenteditable: prevent inner element from being deleted

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.

like image 988
Steve Kim Avatar asked Dec 01 '16 01:12

Steve Kim


People also ask

How does contenteditable work?

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.

How to make HTML element editable?

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.

How do I stop Enter key in Contenteditable?

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.

How do I turn off Contenteditable div?

Just set contentEditable="false" . See this answer.

What does the contentEditable property do?

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.

How do I remove the border around a contentEditable element?

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:

How to make the content of an element editable?

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!";

How to check if 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. 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.


1 Answers

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);
like image 130
David Furlong Avatar answered Oct 18 '22 23:10

David Furlong