1 <html> 2 <div id='textDiv'>Some text we can select</div> 3 <input id='textfield' type="text" value="Text in a textfield"> 4 <script> 5 6 function select_all_text() { 7 var div = document.getElementById("textDiv"); 8 var range = document.createRange(); 9 range.selectNodeContents(div); 10 var sel = window.getSelection(); 11 sel.removeAllRanges(); 12 sel.addRange(range); 13 } 14 15 function get_selection() { 16 return (window.getSelection() + ""); 17 } 18 19 function focus_textfield() { 20 document.getElementById("textfield").focus(); 21 } 22 23 function get_cursor_position() { 24 var div = document.getElementById("textfield"); 25 var start = div.selectionStart; 26 var end = div.selectionEnd; 27 if (start == end) 28 return start; 29 else 30 return -1; 31 } 32 33 </script> 34 35 </html> 36