Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function SelectText(element) {
- var doc = document,
- text = doc.getElementById(element),
- range,
- selection;
- if (doc.body.createTextRange) {
- range = document.body.createTextRange();
- range.moveToElementText(text);
- range.select();
- } else if (window.getSelection) {
- selection = window.getSelection();
- range = document.createRange();
- range.selectNodeContents(text);
- selection.removeAllRanges();
- selection.addRange(range);
- }
- }
- $(function() {
- $('p').click(function() {
- SelectText('selectme');
- });
- });
- $(document).ready(function()
- {
- var words=$("#yourTextContainer").text().split(' ');
- $("#yourTextContainer").html("");
- $.each(words, function(i,val)
- {
- //wrap each word in a span tag
- $('<span/>').text(val +" ").appendTo("#yourTextContainer");
- });
- $("#yourTextContainer span").live("click",function(event)
- {
- event.stopPropagation();
- SelectText($(this));
- });
- });
- function SelectText(element)
- {
- var doc = document,
- text = element.get(0),
- range,
- selection;
- if (doc.body.createTextRange)
- {
- range = document.body.createTextRange();
- range.moveToElementText(text);
- range.select();
- }
- else if (window.getSelection)
- {
- selection = window.getSelection();
- range = document.createRange();
- range.selectNodeContents(text);
- selection.removeAllRanges();
- selection.addRange(range);
- }
- }
- document.onclick = globalOnClickHandler;
- function globalOnClickHandler() {
- var s = window.getSelection();
- var w = s;
- s.modify('move','backward','word');
- w.modify('extend','forward','word');
- //alert(w.toString()); // you can get selected word
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement