Skip to content

Instantly share code, notes, and snippets.

@kingease
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save kingease/4bfffcef5ed178dfca00 to your computer and use it in GitHub Desktop.

Select an option

Save kingease/4bfffcef5ed178dfca00 to your computer and use it in GitHub Desktop.
first try select some words in contentEditable control
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var editor = document.getElementById('main');
editor.focus();
console.dir(editor);
var startNode = editor.childNodes[0];
console.dir(startNode);
var endNode = editor.childNodes[1].firstChild;
var range = document.createRange();
range.setStart(startNode, 4); // 6 is the offset of "world" within "Hello world"
range.setEnd(endNode, 1); // 7 is the length of "this is"
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
var editor=document.getElementById("main");editor.focus();console.dir(editor);var startNode=editor.childNodes[0];console.dir(startNode);var endNode=editor.childNodes[1].firstChild;var range=document.createRange();range.setStart(startNode,4);range.setEnd(endNode,1);var sel=window.getSelection();sel.removeAllRanges();sel.addRange(range);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"jquery": "2.1.4"
}
}
<!-- contents of this file will be placed inside the <body> -->
<h1>riddle editor</h1>
<div contentEditable="true" class="text-area" id="main">
hello <s>world</s>
</div>
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment