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>
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.
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.
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.
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.
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>
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