Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert text in the middle of a textarea 1

Status
Not open for further replies.

grtfercho

Programmer
Apr 11, 2003
424
US
Hi everyone.
I have a form that's used by a group of employees to send out an email to people.
In the form I have a textarea where the body of the message goes and I also have a Combo Box with some pre-set values that come from the database for that particular company. ( company name, company ID, Some specific dates, etc.)
Whenever somebody selects Company Name from the combo box the company name gets inserted at the end of the textarea.
If they select Contact Person then the name of a person gets inserted at the end of textarea.

This is working with no problems. Now my question is ...
If I have a message already in the textarea and move the cursor to someplace in the middle of the message how can I insert the value from the combobox in the middle of the lines?
hope this makes sense. I'll try to get a stripped version of my page and post it later.
in the meantime thanks for any help.



grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
 
OK here it is....
<script>
<!--
function insertVal(theForm){
theForm.fupcomm.value=theForm.fupcomm.value+theForm.dbvalues.options[theForm.dbvalues.options.selectedIndex].value+ &quot; &quot;;
theForm.dbvalues.selectedIndex=0;
theForm.fupcomm.focus();
}
//-->
</script>
<form name=&quot;somename&quot;>
<select name='dbvalues' onChange='insertVal(this.form)'>
<option selected>--- Insert Database Values ---</option>
<option value='06/22/2003'>Due Date</option>
<option value='ABC Solutions'>Company Name</option>
<option value='31189'>Company Id</option>
<option value='20020149'>Case Id</option>
<option value='04/22/2002'>Contact Date</option>
<option value='05/14/2003'>Todays date</option>

</select>
<textarea name=&quot;fupcomm&quot; cols=&quot;70&quot; rows=&quot;9&quot; >Try to select one of the options.</textarea>

</form>


now the idea is to insert the values from the combo Box let's say after the word &quot;Try&quot;.



Is there any property that gives me the position of the cursor inside the Textarea??

thanks in advance

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Wouldn't you want the information appended to the end of the new text in the text area?

(May have an answer for you, just need to clarify)
 
That part is already working if you test the code provided it will append the values with no problems to the end of the text.
Now that was not an issue until someone here forgot to enter a value and wanted to insert it at the beginning of the message.
The best way(the only way actually) to do this has been just to insert the value at the end and from there cut&paste to the desired position.

Could I use onBlur to save the last place where the cursor was and then insert it there?
If yes how could I know what the last position of the cursor was?

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Ah. Where there's a will, there's a way. I doubt if this will work for anything other than IE, though.

<html>
<body>
<script language=&quot;javaScript&quot;>
function storeCaret(obj)
{
if (obj.createTextRange)
obj.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;
textEl.focus();
}
else
textEl.value = text;
}

function insertText(obj)
{
var sValue;
//get the selected value
for (var i=0; i < obj.length; i++)
{
if (obj.selected == true)
{
sValue = obj.value;

}
}

var t = document.getElementById('txtArea');

if (t.caretPos)
{
insertAtCaret(t, sValue);
}
else
{
sValue = &quot;\n&quot; + sValue;
t.value += sValue;
}
}
</script>
<form method=&quot;POST&quot;>
<p><textarea rows=&quot;10&quot; name=&quot;S1&quot; cols=&quot;80&quot; id=&quot;txtArea&quot; ONSELECT=&quot;storeCaret(this);&quot;
ONCLICK=&quot;storeCaret(this);&quot;
ONKEYUP=&quot;storeCaret(this);&quot;>Try to select one of the options</textarea></p>
<p><select size=&quot;1&quot; name=&quot;options&quot; onchange=&quot;insertText(this)&quot;>
<option value=&quot;Due Date&quot;>Opt 1</option>
<option value=&quot;Contact Name&quot;>Opt 2</option>
<option value=&quot;Company Name&quot;>Opt 3</option>
</select></p>
<p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;></p>
</form>
</body>
</html>
 
I found the same script and after making a search for caretPos and createtextRange it seems that this is the only script out there.
I found something that might work on Mozilla 1.3+ @

------------------------------------------------------------
<html> <head> <title> manipulating the selection in an <input type=&quot;text&quot; /> element
</title> <script type=&quot;text/javascript&quot;> function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToEnd (input) {
setSelectionRange(input, input.value.length, input.value.length);
}
function setCaretToBegin (input) {
setSelectionRange(input, 0, 0);
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}
function selectString (input, string) {
var match = new RegExp(string, &quot;i&quot;).exec(input.value);
if (match) {
setSelectionRange (input, match.index, match.index + match
[0].length);
}
}
function replaceSelection (input, replaceString) {
if (input.setSelectionRange) {
var selectionStart = input.selectionStart;
var selectionEnd = input.selectionEnd;
input.value = input.value.substring(0, selectionStart)
+ replaceString
+ input.value.substring(selectionEnd);
if (selectionStart != selectionEnd) // has there been a selection
setSelectionRange(input, selectionStart, selectionStart +
replaceString.length);
else // set caret
setCaretToPos(input, selectionStart + replaceString.length);
}
else if (document.selection) {
var range = document.selection.createRange();
if (range.parentElement() == input) {
var isCollapsed = range.text == '';
range.text = replaceString;
if (!isCollapsed) { // there has been a selection
//it appears range.select() should select the newly
//inserted text but that fails with IE
range.moveStart('character', -replaceString.length);
range.select();
}
}
}
}
</script> </head> <body> <form name=&quot;formName&quot;> <input name=&quot;inputName&quot;
type=&quot;text&quot;
value=&quot;Kibology&quot;
/> <input type=&quot;button&quot;
value=&quot;set selection range to 4, 8&quot;
onclick=&quot;window.setSelectionRange(this.form.inputName, 4, 8);&quot;
/> <input type=&quot;button&quot;
value=&quot;set caret to end&quot;
onclick=&quot;setCaretToEnd(this.form.inputName);&quot;
/> <input type=&quot;button&quot;
value=&quot;set caret to start&quot;
onclick=&quot;setCaretToBegin(this.form.inputName);&quot;
/> <input type=&quot;button&quot;
value=&quot;select string&quot;
onclick=&quot;selectString(this.form.inputName,
prompt('string to search for?', 'bolo'));&quot;
/> <input type=&quot;button&quot;
unselectable=&quot;on&quot;
value=&quot;replace selection with Kibo&quot;
onclick=&quot;replaceSelection (this.form.inputName, 'Kibo');&quot;
/> </form> </body> </html>
------------------------------------------------------------

Thanks for the effort greedy. But all this gives me enough arguments to tell the people here that it can be done but NN users will have problems. That should be enough for them to reconsider.

If anybody has a solution for NN please don't be shy.



grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top