Javascript controls the insertion of text at the cursor position suitable for the insertion of expressions

  • 2020-03-30 03:15:26
  • OfStack

Directly on the code, with js control in the cursor position to insert.

Used for the insertion of expressions.
 
<span style="font-size:18px;"><html> 
<head> 
<script type='text/javascript'> 
function test(str){ 
var tc = document.getElementById("mytextarea"); 
var tclen = tc.value.length; 
tc.focus(); 
if(typeof document.selection != "undefined") 
{ 
document.selection.createRange().text = str; 
} 
else 
{ 
tc.value = tc.value.substr(0,tc.selectionStart)+str+tc.value.substring(tc.selectionStart,tclen); 
} 
} 
</script> 
</head> 
<body> 
<textarea rows=5 name=s1 cols=27 id="mytextarea"> The goal is to click the button on the page button  in textarea Insert text at the cursor stop in  </textarea> 
<input type=button onclick="test(' This is the text that needs to be added ')" /> 
</body> 
</html> 
</span> 

Related articles: