PDA

View Full Version : insert text are cursor position



angst
Mar 23rd, 2007, 06:02 PM
Hello,

I'm currently inserting text via ajax ( asp/javascript ) to a textarea.

I'm wondernig how can i insert the text at the cursor position?

currently, when i insert, it's just being inserted at the end of the existing text.

thanks help would be great!

thanks in advance for your time!
-Ken

Philip M
Mar 23rd, 2007, 08:45 PM
Have a look at

http://www.codingforums.com/showthread.php?t=57217

angst
Mar 23rd, 2007, 08:49 PM
thanks, i actually ment to post back that i had figured it out.

found a nice example:



<html>
<head>
<script type="text/javascript">
function StorePos(ftext) {
if (ftext.createTextRange){
ftext.caretPos = document.selection.createRange().duplicate();
}
}

function insertsmilie(smilieface) {
var t = document.f.t;
if (t.createTextRange && t.caretPos) {
var caretPos = t.caretPos;
caretPos.text = smilieface;
t.focus();
} else {
t.value+=smilieface;
t.focus();
}
}
</script>
<body>
<form name=f>
<textarea rows=10 cols=30 name=t onselect="StorePos(this);" onclick="StorePos(this);" onkeyup="StorePos(this);" onchange="StorePos(this);">Text goes here</textarea><br>
<button onclick="insertsmilie('8)');">Press</button><br>
</form>
</body>
</html>