![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
Regular Coder ![]() Join Date: Aug 2002
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Insert text at cursor position
I'm looking for code that I can use to insert a bit of text into a text box at mouse cursor position (i.e. the smilie buttons used here)
I have found a few examples and tried to use them but none so far that work across IE/Mozilla based browser (mac/pc) Does anyone know of a script or have any suggestions? thanks. |
|
|
|
|
|
PM User | #2 |
|
Banned ![]() Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Try this: (not my code)
Code:
<script type="text/javascript">
function setCaret (textObj) {
if (textObj.createTextRange) {
textObj.caretPos = document.selection.createRange().duplicate();
}
}
function insertAtCaret (textObj, textFeildValue) {
if(document.all){
if (textObj.createTextRange && textObj.caretPos) {
var caretPos = textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?textFeildValue + ' ' : textFeildValue;
}else{
textObj.value = textFeildValue;
}
}else{
if(textObj.setSelectionRange){
var rangeStart = textObj.selectionStart;
var rangeEnd = textObj.selectionEnd;
var tempStr1 = textObj.value.substring(0,rangeStart);
var tempStr2 = textObj.value.substring(rangeEnd);
textObj.value = tempStr1 + textFeildValue + tempStr2;
}else{
alert("This version of Mozilla based browser does not support setSelectionRange");
}
}
}
</script>
<form id="form1" 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);" >
Click in this textObj anywhere and then press the button below.
It will insert the text at the caret position.</textarea>
<br/><br/>
<input type="text" name="textfield" style="width:220px;" value="Mozilla 1.4"/>
<br/>
<input type="button" value="insert at caret"
onclick="insertAtCaret(this.form.tarea,this.form.textfield.value);"/>
</p>
</form>
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|