Here's the code:
Copy Code code as follows:
--------------------------------------------------------------------------------
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>js inserts text at the text field mouse specified position-ke keeper </title>
<script type= "Text/javascript" >
function Insertatcursor (MyField, myvalue) {
IE Support
if (document.selection) {
Myfield.focus ();
sel = Document.selection.createRange ();
Sel.text = myvalue;
Sel.select ();
}
Mozilla/netscape Support
else if (Myfield.selectionstart | | myfield.selectionstart = = ' 0 ') {
var startpos = Myfield.selectionstart;
var endpos = myfield.selectionend;
Save scrolltop before insert www.keleyi.com
var restoretop = myfield.scrolltop;
Myfield.value = myField.value.substring (0, startpos) + myvalue + myField.value.substring (endpos, MyField.value.length) ;
if (Restoretop > 0) {
Myfield.scrolltop = Restoretop;
}
Myfield.focus ();
Myfield.selectionstart = Startpos + myvalue.length;
Myfield.selectionend = Startpos + myvalue.length;
} else {
Myfield.value + = myvalue;
Myfield.focus ();
}
}
</script>
<body>
<div style= "width:500px;margin-left:auto;margin-right:auto;margin-top:100px;" >
<textarea id= "keleyi_com" style= "width:340px; height:180px ">
Collayi Javascript inserts text at TEXTAREA cursor
</textarea>
<input type= "button" onclick= "Insertatcursor (document.getElementById (' keleyi_com '), ' www.keleyi.com ')" value= " Insert Text "/>
</div></body>