title: Programmatically select text in a contenteditable HTML element?
body: <p>In JavaScript, it's possible to programmatically select text in an <code>input</code> or <code>textarea</code> element. You can focus an input with <code>ipt.focus()</code>, and then select its contents with <a href="https://developer.mozilla.org/en/DOM/Input.select" rel="noreferrer"><code>ipt.select()</code></a>. You can even select a specific range with <a href="https://developer.mozilla.org/en/DOM/Input.setSelectionRange" rel="noreferrer"><code>ipt.setSelectionRange(from,to)</code></a>.</p>
  
  <p>My question is: is there any way to do this in a <code>contenteditable</code> element too? </p>
  
  <p>I found that I can do <code>elem.focus()</code>, to put the caret in a <code>contenteditable</code> element, but subsequently running <code>elem.select()</code> doesn't work (and nor does <code>setSelectionRange</code>). I can't find anything on the web about it, but maybe I'm searching for the wrong thing...</p>
  
  <p>By the way, if it makes any difference, I only need it to work in Google Chrome, as this is for a Chrome extension.</p>