SoFunction
Updated on 2025-02-28

JavaScript inserts other text at the cursor in a text

For example: There is a text in a text box. I want to click the mouse and insert the text in the corresponding place
I did this: first get the value of the text box, then .+ other text



<script type="text/javascript">   
function setCaret(textObj){  
  if(){    
    =().duplicate();    
  }  
}

function insertAtCaret(textObj,textFeildValue){  
  if(){    
    if(&&){      
      var caretPos=;      
      =(-1)==''?textFeildValue+'':textFeildValue;
    }else {      
      =textFeildValue;      
    }    
  }else {    
    if(){      
      var rangeStart=;      
      var rangeEnd=;      
      var tempStr1=(0,rangeStart);      
      var tempStr2=(rangeEnd);      
      =tempStr1+textFeildValue+tempStr2;      
    }else {      
      alert("This version of Mozilla based browser does not support setSelectionRange");      
    }    
  }  
}    
</script>   

<form  action="" onsubmit="" method="post" enctype="text/plain">     
<p>   
<textarea name="tarea" rows="" cols="" style="width:300px;height:120px;" onselect="setCaret(this);" onclick="setCaret(this);" onkeyup="setCaret(this);">
  
</textarea>   
<br/><br/>   
<input type="text" name="textfield" style="width:220px;" value="Insert FireFox"/>
<br/>   
<input type="button" value="insert" onclick="insertAtCaret(,);"/>
</p>   
</form>