Posted By

charliefmoran on 04/25/12


Tagged

form text


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hurcy


select text in form element


 / Published in: JavaScript
 

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

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

  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. }

Report this snippet  

You need to login to post a comment.