Skip to content

Instantly share code, notes, and snippets.

@gtb104
Created October 30, 2013 02:21
Show Gist options
  • Save gtb104/7226269 to your computer and use it in GitHub Desktop.
Save gtb104/7226269 to your computer and use it in GitHub Desktop.
<div id="ta" contentEditable="true"></div>
var ta = document.getElementById("ta");
ta.addEventListener("keyup", foo, false);
function foo(e) {
var text = e.target.innerText;
if (text.length >= 10) {
f = text.slice(0,10);
str = f + "<em>" + text.slice(10) + "</em>";
e.target.innerHTML = str;
range = document.createRange();
range.selectNodeContents(ta);
range.collapse(false);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
}
div {
border: 1px solid black;
width: 200px;
height: 100px;
overflow-y: scroll;
overflow-x: hidden;
line-break: word;
}
em {
font-style: normal;
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment