jquery select text


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



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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.