Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get innerHTML of #document-fragment instead of textContent

I have a contenteditable p tag. When the content from the p tag is extracted, and inserted into the other tag, all the markup of the original is lost. Is there any way to save the markup?

Please note if the caret is @ the i-th position in the paragraph, then the characters are extracted starting from that position till the end.

function select() {
    var el = document.getElementById('p');
    el.focus();

    var sel = window.getSelection();
    var selRange = sel.getRangeAt(0);

    var range = selRange.cloneRange();
    range.selectNodeContents(el);
    range.setStart(selRange.endContainer, selRange.endOffset);

    document.getElementById('other').innerHTML = (range.extractContents().textContent);
    // return range.extractContents().textContent;
}
p {
	background-color: #eee;
}

.red {
  color: red;
}
<p id="p" contenteditable="true">This is <i>a</i> <span class="red">paragraph</span> <b>with</b> lots of markup.</p>
<p id="other"></p>
<button onclick="select()">SELECT</button>
like image 673
dorado Avatar asked Apr 15 '16 17:04

dorado


People also ask

How do I get the innerHTML value?

Firstly, to get the innerHTML value of any tag, you either need that tag to have its 'id' property or 'name' property set. Then you can respectively use the 'document. getElementById(yourTagIdValue). innerHTML' or 'document.

What is innerHTML in HTML?

The innerHTML property returns: The text content of the element, including all spacing and inner HTML tags. The innerText property returns: Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements.

What can I use instead of innerHTML?

HTML specifies that a <script> tag inserted with innerHTML should not execute. For that reason, it is recommended that instead of innerHTML you use: Element. SetHTML() to sanitize the text before it is inserted into the DOM.

What is innerHTML in JS?

The innerHTML property can be used to write the dynamic html on the html document. It is used mostly in the web pages to generate the dynamic html such as registration form, comment form, links etc.


1 Answers

range.extractContents() returns a DocumentFragment.

DocumentFragment doesn't have an innerHTML property. innerHTML comes from the Element interface, while textContent is part of the Node interface. DocumentFragment inherits from Node, but not Element.

This means you could just append the entire fragment because it's a Node:

function select() {
  var el = document.getElementById('p');
  el.focus();

  var sel = window.getSelection();
  var selRange = sel.getRangeAt(0);

  var range = selRange.cloneRange();
  range.selectNodeContents(el);
  range.setStart(selRange.endContainer, selRange.endOffset);

  //document.getElementById('other').innerHTML = (range.extractContents().textContent);
  var frag = range.extractContents();
  var i;
  var node;
  var other = document.getElementById('other');
  other.innerHTML = '';
  other.appendChild(frag);
}
p {
	background-color: #eee;
}

.red {
  color: red;
}
<p id="p" contenteditable="true">This is <i>a</i> <span class="red">paragraph</span> <b>with</b> lots of markup.</p>
<p id="other"></p>
<button onclick="select()">SELECT</button>
like image 51
zzzzBov Avatar answered Sep 28 '22 15:09

zzzzBov