English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Method to Get and Set the Cursor Position

1. Get cursor position:

// Get cursor position
function getCursortPosition (textDom) {
 var cursorPos = 0;
 if (document.selection) {
  // IE Support
  textDom.focus ();
  var selectRange = document.selection.createRange();
  selectRange.moveStart ('character', -textDom.value.length);
  cursorPos = selectRange.text.length;
 } else if (textDom.selectionStart || textDom.selectionStart == '0') {}}
  // Firefox Support
  cursorPos = textDom.selectionStart;
 }
 return cursorPos;
}

2. Set cursor position:

// Set cursor position
function setCaretPosition(textDom, pos){
 if(textDom.setSelectionRange) {
  // IE Support
  textDom.focus();
  textDom.setSelectionRange(pos, pos);
 } else if (textDom.createTextRange) {
  // Firefox Support
  var range = textDom.createTextRange();
  range.collapse(true);
  range.moveEnd('character', pos);
  range.moveStart('character', pos);
  range.select();
 }
}

3. Get selected text:

// Get selected text
function getSelectText() {
 var userSelection, text;
 if (window.getSelection) {
  // Firefox Support
  userSelection = window.getSelection();
 } else if (document.selection) {
  // IE Support
  userSelection = document.selection.createRange();
 }
 if (!(text = userSelection.text)) {
  text = userSelection;
 }
 return text;
}

4. Select text within a specific range:

/**
* Select text within a specific range
* Parameters:
*  textDom [JavaScript DOM String] The current object
*  startPos [Int] Starting Position
*  endPos [Int] End position
*/
function setSelectText(textDom, startPos, endPos) {
 var startPos = parseInt(startPos),
  endPos = parseInt(endPos),
  textLength = textDom.value.length;
 if(textLength){
  if(!startPos){
   startPos = 0;
  }
  if(!endPos){
   endPos = textLength;
  }
  if(startPos > textLength){
   startPos = textLength;
  }
  if(endPos > textLength){
   endPos = textLength;
  }
  if(startPos < 0){
   startPos = textLength + startPos;
  }
  if(endPos < 0){
   endPos = textLength + endPos;
  }
  if(textDom.createTextRange){
   // IE Support
   var range = textDom.createTextRange();
   range.moveStart("character",-textLength);
   range.moveEnd("character",-textLength);
   range.moveStart("character", startPos);
   range.moveEnd("character",endPos);
   range.select();
  }
   // Firefox Support
   textDom.setSelectionRange(startPos, endPos);
   textDom.focus();
  }
 }
}

5. Insert text after the cursor:

/**
* Insert text after the cursor
* Parameters:
*  textDom [JavaScript DOM String] The current object
*  value [String] The text to be inserted
*/
function insertAfterText(textDom, value) {
 var selectRange;
 if (document.selection) {
  // IE Support
  textDom.focus();
  selectRange = document.selection.createRange();
  selectRange.text = value;
  textDom.focus();
 } else if (textDom.selectionStart || textDom.selectionStart == '0') {}}
  // Firefox Support
  var startPos = textDom.selectionStart;
  var endPos = textDom.selectionEnd;
  var scrollTop = textDom.scrollTop;
  textDom.value = textDom.value.substring(0, startPos) + value + textDom.value.substring(endPos, textDom.value.length);
  textDom.focus();
  textDom.selectionStart = startPos + value.length;
  textDom.selectionEnd = startPos + value.length;
  textDom.scrollTop = scrollTop;
 }
 else {
  textDom.value += value;
  textDom.focus();
 }
}

That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to support the呐喊 tutorial more!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like