Skip to content

Instantly share code, notes, and snippets.

@veer66
Created March 4, 2013 17:03
Show Gist options
  • Save veer66/5083737 to your computer and use it in GitHub Desktop.
Save veer66/5083737 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Selecting text via API</title>
</head>
<body>
<div><span id="s1">ABCDEFGHI</span><span id="s2">12345</span></div>
<script>
var span = document.getElementById("s1");
var text = span.childNodes[0];
var r = document.createRange();
r.setStart(text, 2);
r.setEnd(text, 6);
window.getSelection().addRange(r);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment