This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 28th, 2012  |  syntax: None  |  size: 1.19 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.   function getRangePos(obj){
  2.     var Pos = 0; //ie
  3.     if (document.selection){
  4.       obj.focus();
  5.       var Sel = document.selection.createRange();
  6.       Sel.moveStart('character', -obj.value.length);
  7.       Pos = Sel.text.length;
  8.     }else if (obj.selectionStart || obj.selectionStart == '0'){
  9.       Pos = obj.selectionStart;
  10.     }
  11.    
  12.     return Pos;
  13.   }
  14.  
  15.   function setRangePos(obj, pos){
  16.     if (obj.setSelectionRange){
  17.       obj.focus();
  18.       obj.setSelectionRange(pos,pos);
  19.     }else if (obj.createTextRange){
  20.       var range = obj.createTextRange();
  21.       range.collapse(true);
  22.       range.moveEnd('character', pos);
  23.       range.moveStart('character', pos);
  24.       range.select();
  25.     }
  26.   }
  27.  
  28.   function get_click(){
  29.     var obj = document.getElementById('soruce_id');
  30.     var pos = getRangePos(obj);
  31.     document.getElementById('info').innerHTML = document.getElementById('info').innerHTML  + 'Pos:' + pos +  '\n';
  32.     obj.focus();
  33.   }
  34.  
  35.   function set_click(){
  36.     var obj = document.getElementById('soruce_id');
  37.     var num = document.getElementById('pos_num');
  38.     var numVal = parseInt(num.value);
  39.     num.value = numVal;
  40.     if (numVal > 0){
  41.       setRangePos(obj, numVal);
  42.     }
  43.     obj.focus();
  44.   }
clone this paste RAW Paste Data