[Javascript] Insert text into textbox at cursor's position? - 15 NP$ REWARD

Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
SpaceshipSpaceship
Watch

user-7256

VIP Member
Impact
111
Hey,

I've found some samples online but none of them work... or at least in Firefox.

My form is named "form1" and my textbox is named "tarea".

Can anybody find some code that WORKS on BOTH browsers? (IE and FF).

Kudos, I mean, 15 NP$ to you if it works properly :)

Cheers,
-Matt
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I found this, I've not tried it though.

http://www.codingforums.com/archive/index.php/t-44679.html
HTML:
<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>
 
0
Close
Yeah that's one that didn't work -_-
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
CryptoExchange.com
Domain Recover

NamePros updates

Quick replies: search, your most used replies, and more on every post
Quick replies are easier to find and use now. The quick reply menu has a search box. Type a few letters to find the reply you want. A...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back