|
-
May 14th, 2003, 04:15 PM
#1
Thread Starter
Stuck in the 80s
[Resolved] JavaScript: Insert into TextArea?
Is there a way to insert text into a TextArea at the caret position, rather than at the end of the box?
Last edited by The Hobo; May 27th, 2003 at 03:13 PM.
-
May 27th, 2003, 04:51 AM
#2
Re: JavaScript: Insert into TextArea?
Originally posted by The Hobo
Is there a way to insert text into a TextArea at the caret position, rather than at the end of the box?
here you go, i put together a example for you.
Hope this helps.
Danial
Code:
<html>
<head>
<script>
function insert(txt)
{
var d;
d=document.form1.txtBody;
insertAtCaret(d, txt);
}
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
textEl.value =textEl.value+ text;
}
</script>
<body>
<a href="JavaScript:insert(' test ')">Insert Text</a>
<form name=form1>
<textarea name="txtBody" rows="20" cols="60" style="WIDTH: 100%" ONSELECT="storeCaret(this);" ONCLICK="storeCaret(this);" ONKEYUP="storeCaret(this);">
Hello there, this is an example of how to enter text at the caret position.
</textarea>
</form>
</body>
</html>
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
May 27th, 2003, 04:52 AM
#3
BTW, i have only tested this in IE not in any browser, please let me know if it works on any other browser.
thanks
Danial
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
May 27th, 2003, 03:08 PM
#4
Thread Starter
Stuck in the 80s
Works perfectly in IE. Thanks
-
May 27th, 2003, 03:13 PM
#5
Thread Starter
Stuck in the 80s
I had to doctor it up a bit, though.
-
May 27th, 2003, 03:17 PM
#6
Originally posted by The Hobo
I had to doctor it up a bit, though.
Well like i said it was an example, i took it out of one of my project, and it was broken down in diffrent files and in the example here i cut and pasted it. so there might be erros. !!!
What bits did u doctor up !!
Anyway glad i could help u out.
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
May 28th, 2003, 01:36 AM
#7
I'm quite sure this works ONLY in IE though, but can't test right now.
Not even sure if this thing is possible in Moz, would have to check...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 28th, 2003, 02:05 AM
#8
Checked the Mozilla source, there is currently no special support for textarea manipulations.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 28th, 2003, 03:07 PM
#9
Originally posted by CornedBee
Checked the Mozilla source, there is currently no special support for textarea manipulations.
Yes i remember now that i looked all over to find some code which will achieve the same thing in NS and other browser., but was unable to find any.
I doubt its even possible, i think even vB Forum doesnt have the feature in NS.
Danial
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Jun 21st, 2004, 03:43 PM
#10
Thread Starter
Stuck in the 80s
I revisited this because I didn't like the fact that this feature only worked in IE for my application. After doing some searching on the web, I've put together this code.
The code was tested (and works fine) in:
FireFox 0.8
Netscape 7.1
Internet Explorer 5.5/6.0
Mozilla 1.7
Code:
<html>
<head>
<title></title>
<script type="text/javascript">
function insertAtCaret(input, textString) {
if (input.setSelectionRange) {
var selectionStart = input.selectionStart;
var selectionEnd = input.selectionEnd;
input.value = input.value.substring(0, selectionStart) + textString + input.value.substring(selectionEnd);
} else if (document.selection) {
var range = document.selection.createRange();
if (range.parentElement() == input) {
var isCollapsed = range.text == '';
range.text = textString;
if (!isCollapsed) {
range.moveStart('character', -textString.length);
range.select();
}
} else {
var caretPos = input.caretPos;
caretPos.text = textString;
}
} else {
input.value = input.value + textString;
}
}
function storeCaret (input) {
if (input.createTextRange) {
input.caretPos = document.selection.createRange().duplicate();
}
}
</script>
</head>
<body>
<form name="formName">
<input type="button" value=" insert " onclick="insertAtCaret(this.form.inputName, 'asdf');" />
<br />
<textarea cols="20" rows="10" name="inputName" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" style="width: 400px;">Kibology</textarea>
</form>
</body>
</html>
Let me know if there are any improvements/suggestions. Otherwise, there it is for whoever's interested.
Also, if anyone tests this in a browser/version that I haven't, please let me know how it goes.
-
Jun 21st, 2004, 03:47 PM
#11
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 22nd, 2004, 10:19 AM
#12
Frenzied Member
Originally posted by CornedBee
Moz 1.8a works fine.
Isn't it the same version of Gecko? Does it really matter if you test it in both normal 'heavy' MoZilla and FireFox ?
"Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
- Zack de la Rocha
Hear me roar.
-
Jun 22nd, 2004, 12:14 PM
#13
More or less the same, yeah. Just wanted to post
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 22nd, 2004, 05:07 PM
#14
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|