select text in form element


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

cross-browser way to select all text in a form element, via Jason on Stack Overflow


Copy this code and paste it in your HTML
  1. function selectText(element) {
  2. var doc = document;
  3. var text = doc.getElementById(element);
  4.  
  5. if (doc.body.createTextRange) { // ms
  6. var range = doc.body.createTextRange();
  7. range.moveToElementText(text);
  8. range.select();
  9. } else if (window.getSelection) { // moz, opera, webkit
  10. var selection = window.getSelection();
  11. var range = doc.createRange();
  12. range.selectNodeContents(text);
  13. selection.removeAllRanges();
  14. selection.addRange(range);
  15. }
  16. }

URL: http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.