Skip to content

Instantly share code, notes, and snippets.

@carlcarl
Created March 7, 2012 20:33
Show Gist options
  • Save carlcarl/1995926 to your computer and use it in GitHub Desktop.
Save carlcarl/1995926 to your computer and use it in GitHub Desktop.
javascript text selection
<head>
<script type="text/javascript">
function GetSelectedText () {
if (window.getSelection) { // Firefox, Opera, Google Chrome and Safari
var range = window.getSelection ();
alert (range.toString ());
}
else {
if (document.selection.createRange) { // Internet Explorer
var range = document.selection.createRange ();
alert (range.text);
}
}
}
</script>
</head>
<body>
<button onclick="GetSelectedText ()">Get the selected text!</button>
Select some text!
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment