Created
September 7, 2012 12:49
-
-
Save phawk/3665994 to your computer and use it in GitHub Desktop.
Selects text of an element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doc = { | |
selectText: function(jqSelector) { | |
var text = jqSelector.get(0); | |
if ($.browser.msie) { | |
var range = document.body.createTextRange(); | |
range.moveToElementText(text); | |
range.select(); | |
} else if ($.browser.mozilla || $.browser.opera) { | |
var selection = window.getSelection(); | |
var range = document.createRange(); | |
range.selectNodeContents(text); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} else if ($.browser.safari || $.browser.chrome) { | |
var selection = window.getSelection(); | |
selection.setBaseAndExtent(text, 0, text, 1); | |
} | |
} | |
}; | |
$(document).ready(function() { | |
// Example usage | |
doc.selectText($("#some-element")); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment