jquery select text


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // solution found here http://jsfiddle.net/edelman/KcX6A/1/
  2.  
  3. <div id="selectme" contenteditable='true'>Some text goes here!</div>
  4. <p>Click me!</p>
  5.  
  6. function SelectText(element) {
  7. var text = document.getElementById(element);
  8. if ($.browser.msie) {
  9. var range = document.body.createTextRange();
  10. range.moveToElementText(text);
  11. range.select();
  12. } else if ($.browser.mozilla || $.browser.opera) {
  13. var selection = window.getSelection();
  14. var range = document.createRange();
  15. range.selectNodeContents(text);
  16. selection.removeAllRanges();
  17. selection.addRange(range);
  18. } else if ($.browser.safari) {
  19. var selection = window.getSelection();
  20. selection.setBaseAndExtent(text, 0, text, 1);
  21. }
  22. }
  23.  
  24. $(function() {
  25. $('p').click(function() {
  26. SelectText('selectme');
  27. });
  28. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.