JSFiddle - React, Tailwind, and code Playground
HTML
<input type="button" value="underline" onclick="underline()">
<div id="test" contenteditable="true">This <i>is my</i><b>sample</b> text.</div>
<textarea id="html" rows="8" cols="50"></textarea>
JavaScript
function underline() {
// Next line ensures <u> elements are used in stead of <span>s with CSS text-decoration
document.execCommand("StyleWithCSS", false, false);
document.execCommand("Underline", false, null);
showHtml();
}
function showHtml() {
document.getElementById("html").value = document.getElementById("test").innerHTML;
}
window.onload = showHtml;