I have the following simple contenteditable
table cell:
When I press the Test
button, it outputs the innerHTML
and innerText
values inside the box.
innerHTML
: (note the 4 <br>
tags at the end)
<i style="font-size: 14.6667px;">This</i><span style="font-size: 14.6667px;"> is one line</span><br style="font-size: 14.6667px;"><br style="font-size: 14.6667px;"><span style="font-size: 14.6667px;">This is </span><u style="font-size: 14.6667px; font-weight: bold;">another</u><span style="font-size: 14.6667px;"> </span><span style="font-size: 14.6667px;">line</span><br style="font-size: 14.6667px;"><br><br><br style="font-size: 14.6667px;">
innerText
:
This is one line
This is another line
What I want to achieve: be able to "trim" the innerHTML value (i.e., remove blank lines) while keeping the formatting. This is simple to do with the innerText value (just something like document.getElementById("id").innerText.trim()
), but the formatting is gone. I can't see any reference to "trim" an innerHTML value. Is there a way to do this using either Javascript or JQuery?
function test() {
var html = document.getElementById("inputBox").innerHTML;
var text = document.getElementById("inputBox").innerText;
console.log(`html:\n${html}`);
console.log(`text:\n${text}`);
}
table {
width: 100%;
}
td#inputBox {
height: 100px;
border: 1px solid #020202;
}
<table>
<tr>
<td contenteditable="true" id="inputBox"></td>
</tr>
</table>
<button onclick="test()">Test</button>
I have the following simple contenteditable
table cell:
When I press the Test
button, it outputs the innerHTML
and innerText
values inside the box.
innerHTML
: (note the 4 <br>
tags at the end)
<i style="font-size: 14.6667px;">This</i><span style="font-size: 14.6667px;"> is one line</span><br style="font-size: 14.6667px;"><br style="font-size: 14.6667px;"><span style="font-size: 14.6667px;">This is </span><u style="font-size: 14.6667px; font-weight: bold;">another</u><span style="font-size: 14.6667px;"> </span><span style="font-size: 14.6667px;">line</span><br style="font-size: 14.6667px;"><br><br><br style="font-size: 14.6667px;">
innerText
:
This is one line
This is another line
What I want to achieve: be able to "trim" the innerHTML value (i.e., remove blank lines) while keeping the formatting. This is simple to do with the innerText value (just something like document.getElementById("id").innerText.trim()
), but the formatting is gone. I can't see any reference to "trim" an innerHTML value. Is there a way to do this using either Javascript or JQuery?
function test() {
var html = document.getElementById("inputBox").innerHTML;
var text = document.getElementById("inputBox").innerText;
console.log(`html:\n${html}`);
console.log(`text:\n${text}`);
}
table {
width: 100%;
}
td#inputBox {
height: 100px;
border: 1px solid #020202;
}
<table>
<tr>
<td contenteditable="true" id="inputBox"></td>
</tr>
</table>
<button onclick="test()">Test</button>
Share
Improve this question
edited Feb 17 at 6:49
DarkBee
15.6k8 gold badges72 silver badges116 bronze badges
asked Feb 17 at 6:27
ajthealchemistajthealchemist
12710 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0You can use DOMParser to loop over the elements and remove what needs to be removed:
function removeTrailingBr(html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
let lastChild = doc.body.lastChild;
while (lastChild?.nodeType === Node.ELEMENT_NODE && lastChild.tagName === "BR") {
const toRemove = lastChild;
lastChild = lastChild.previousSibling;
doc.body.removeChild(toRemove);
}
return doc.body.innerHTML;
}
const html = `<i style="font-size: 14.6667px;">This</i><span style="font-size: 14.6667px;"> is one line</span><br style="font-size: 14.6667px;"><br style="font-size: 14.6667px;"><span style="font-size: 14.6667px;">This is </span><u style="font-size: 14.6667px; font-weight: bold;">another</u><span style="font-size: 14.6667px;"> </span><span style="font-size: 14.6667px;">line</span><br style="font-size: 14.6667px;"><br><br><br style="font-size: 14.6667px;">`;
console.log(removeTrailingBr(html));
<br>
elements from the fragment and append the fragment to the contenteditable. – Teemu Commented Feb 17 at 6:35DOMPurify.sanitize(dirtyHTML, {ALLOWED_TAGS: ['i', 'u'], ALLOWED_ATTR: []});
– Diego D Commented Feb 17 at 11:30