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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting char position inside textArea

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
Hello, I need help with the following function Please:

function saveCaret(elem)
{
if( elem.isTextEdit )
{
elem.caretPos = document.selection.createRange();
}
}

var selectionStartChar
function getCaretPos(elem)
{
if ( elem.isTextEdit && elem.caretPos )
{
var orig = elem.value;
alert (orig);
var bookmark = "~";
var caretPos = elem.caretPos;
caretPos.text = bookmark;
selectionStartChar = elem.value.search( bookmark );
window.status = "Caret is at character " + selectionStartChar;
//window.document.writeln ("Caret is at character " + selectionStartChar);
alert ("Users selection starts at char " + selectionStartChar + " (this is the selection we want to move)" );
elem.value = orig;
}

}



it gets called:

<textarea name="ta1" rows="13" cols="60" onSelect="saveCaret(this);">

<input name="btn" type="button" onclick="getCaretPos(ta1)" value="Move Selected Text">



What I am trying to achieve is to hold the 'selected value' at the same time as detecting the char position of the strat of the user selection. By the time it gets into the second function, to the line: alert (orig); my selection has been lost - I still get the char position I am looking for

Thank you in advance

Here is the complete cut n paste code .html. I have achieved some usefull functionality, please copy n run the page. My objective: there are two text areas. Each area has section headers EG:Issues (it will become clear if you run the page) what I am trying to do is: A user selects some text form a section, when they press the move text button, the text will be copied into the same section in the above textarea, (please dont let the length of the code put you off running it, i am not a js techo - i'm sure a techo will spot my error easily - alerts walk you thru what I have working, 95% is working!!) Here is what I have working. OnSelect of the second text area a function runs to createRange. Then on click of the moveText button four function run to detect the char positions of the sections, the first char of the user selected text string and what section it came from. When the last function runs to move the text - All of it moves, please help me - it would be hugely appreciated.

--------Thanking you in advance------------------start cunt n paste code---------

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
//make array global arrays so the elements maybe used in other functions
var myLabelsArray = new Array("EVENTS:","ISSUES:","VISITS:","REPUTATION:","MINISTERIALS:","PERSONNAL:","ACHIEVEMENTS:");
var myCharPositions = new Array(6);

//first function called:get position of section values (Events etc..) values
function getPosOfStringValues(elem)
{
var result;
var i = 0;
while (i <= 6)
{
result = elem.value.search(myLabelsArray);
myCharPositions = result;
alert (myLabelsArray + " is at character " + myCharPositions)
i++;
}
}

//second function called, run on select of textarea:create range for position
function saveCaret(elem)
{
if( elem.isTextEdit )
{
elem.caretPos = document.selection.createRange();
}
}

//third function called:get char position for first char of selection
var selectionStartChar
function getCaretPos(elem)
{
if ( elem.isTextEdit && elem.caretPos )
{
var bookmark = "~";
var orig = elem.value;
var caretPos = elem.caretPos;
caretPos.text = bookmark;
selectionStartChar = elem.value.search( bookmark );
window.status = "Caret is at character " + selectionStartChar;
//window.document.writeln ("Caret is at character " + selectionStartChar);
alert ("Users selection starts at char " + selectionStartChar + " (this is the selection we want to move)" );
elem.value = orig;
}

}

//fourth function called: selectionStartChar = char pos of selection
function whereIsSelection()
{
if((selectionStartChar > myCharPositions[0]) && (selectionStartChar < myCharPositions[1]))
{
alert ("came from the section:" + myLabelsArray[0] + " needs to be copied into the same section in the above text area");
}
if((selectionStartChar > myCharPositions[1]) && (selectionStartChar < myCharPositions[2]))
{
alert ("came from the section:" + myLabelsArray[1] + " needs to be copied into the same section in the above text area");
}
if((selectionStartChar > myCharPositions[2]) && (selectionStartChar < myCharPositions[3]))
{
alert ("came from the section:" + myLabelsArray[2] + " needs to be copied into the same section in the above text area");
}
if((selectionStartChar > myCharPositions[3]) && (selectionStartChar < myCharPositions[4]))
{
alert ("came from the section:" + myLabelsArray[3] + " needs to be copied into the same section in the above text area");
}
if((selectionStartChar > myCharPositions[4]) && (selectionStartChar < myCharPositions[5]))
{
alert ("came from the section:" + myLabelsArray[4] + " needs to be copied into the same section in the above text area");
}
if((selectionStartChar > myCharPositions[5]) && (selectionStartChar < myCharPositions[6]))
{
alert ("came from the section:" + myLabelsArray[5] + " needs to be copied into the same section in the above text area");
}
if(selectionStartChar > myCharPositions[6])
{
alert ("came from the section:" + myLabelsArray[6] + " needs to be copied into the same section in the above text area");
}
}

//fifth and final function called
//code to swap selected text from textArea to textArea
var copyStr;
var lbr;
function moveSelection(where)
{
lbr='';
if (document.selection)
{
tmp=where.value;
alert (tmp);
sel = document.selection.createRange();
copyStr=sel.text;
document.selection.clear();
tmpx=where.value.replace(/\r\n/g,' ');
tmpy=tmp.replace(/\r\n/g,' ');
tmpz=tmpy.length-tmpx.length-copyStr.length;
for (var i=0;i<tmpz;i++)
{
lbr+='\r\n';
}
}
else if (where.selectionStart || where.selectionStart == '0')
{
var begin = where.selectionStart;
var end = where.selectionEnd;
copyStr=where.value.substring(begin,end);
where.value=where.value.substring(0,begin)+where.value.substring(end,where.value.length)
}
if(copyStr=="")
{
alert('No text selected to be moved!')
}
else
{
document.forms[0].briefString.value+=copyStr+lbr;
}
}


/*
//used for testing mt two global Arrays
//(array) myLabelsArray = actual section labels
//(array) myCharPositions = char locations of sections in forwarded brief(s)
//(var) selectionStartChar = char position at start of users selection (to move)
function testValues()
{
var i = 0
while (i <= 6)
{
alert (myLabelsArray + " is at character " + myCharPositions)
i++;
}
}
*/
//-->
</script>
</head>
<body>
<form>
<textarea name="briefString" rows="13" cols="60">EVENTS:

ISSUES:

VISITS:

REPUTATION:

MINISTERIALS:

PERSONNAL:

ACHIEVEMENTS:</textarea>
<br><br>
<textarea name="ta1" rows="13" cols="60" onSelect="saveCaret(this);">
EVENTS:
eeeee eeeee eeeeee eeeeee
ISSUES:
iiiii iiiii iiiii iiiiii
VISITS:
vvvvv vvvvv vvvvv vvvvv
REPUTATION:
rrrrr rrrrrr rrrrr rrrrrr
MINISTERIALS:
mmmmm mmmmm mmmmm mmmmm
PERSONNAL:
ppppp ppppp ppppp ppppp
ACHIEVEMENTS:
aaaaa aaaaa aaaaa aaaaa
</textarea><br><br>
<input name="btn" type="button" onclick="getPosOfStringValues(ta1),getCaretPos(ta1),whereIsSelection(),moveSelection(this.form.ta1);" value="Move Selected Text">
<br><br>


</form>


</body>
</html>-----------------------------finish------------------------------------
 
You can use the selection object to hold the value of what the user selected in the textarea:
Code:
<script>
<!--
function GetSelection() {
	range = document.selection.createRange();
	alert(range.text);
}
//-->
</script>

<textarea>Hi, I'm text.</textarea>
<br>
<input type="button" value="Get Selection" onClick="GetSelection()">
 
Supra

Unbelievable - I managed to do it the moment your email came in

Mate, i'm no .js guy I'm pulling my hair out with this one (getting the text into the same section in the top area) Did you run the file and get the jist of what i'm trying to do? Ive come along way for an asp guy - I'm in need of a little help finishing

Thanks for your time
 
I see what you're trying to do I think. You're trying to take the selected text from the user and detect what category it came from. Then, take the selected text and append it to the same category in the textarea above. Is this correct?
 
Yes, absolutly spot on

I have implimetnted your suggestion, removed all the random alerts - it's 97.5% there. So close, here is the better version. Oh by the way it's an ASP page, i removed all the :<%= vbcrlf & vbcrlf %> (used purely for formattin inside the text areas to post it as a .html) if you arnt running iis - changing it to a .html is fine (formatting will be all thats lost). mate your a legend - that little snippet you posted worked like a charm - i am so close, is my .js a little inefficient??? the only function I didnt writ was moveSelection (very last one)
----------------start code-----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
//make array global arrays so the elements maybe used in other functions
var myLabelsArray = new Array("EVENTS:","ISSUES:","VISITS:","REPUTATION:","MINISTERIALS:","PERSONNAL:","ACHIEVEMENTS:");
var myCharPositions = new Array(6);

//first function called:get position of section values (Events etc..) values
function getPosOfStringValues(elem)
{
var result;
var i = 0;
while (i <= 6)
{
result = elem.value.search(myLabelsArray);
myCharPositions = result;
//alert (myLabelsArray + " is at character " + myCharPositions)
i++;
}
}

//second function:get selected text
var selectionForMove
function GetSelectedTxt()
{
range = document.selection.createRange();
selectionForMove = range.text;
//alert("selection = " + selectionForMove);
}

//third function called, run on select of textarea:create range for position
function saveCaret(elem)
{
if( elem.isTextEdit )
{
elem.caretPos = document.selection.createRange();
}
}

//fourth function called:get char position for first char of selection
var selectionStartChar
function getCaretPos(elem)
{
if ( elem.isTextEdit && elem.caretPos )
{
var orig = elem.value;
var bookmark = "~";
var caretPos = elem.caretPos;
caretPos.text = bookmark;
selectionStartChar = elem.value.search( bookmark );
window.status = "Caret is at character " + selectionStartChar;
//window.document.writeln ("Caret is at character " + selectionStartChar);
//alert ("Users selection starts at char " + selectionStartChar + " (this is the selection we want to move)" );
elem.value = orig;
}

}

//fifth function called: selectionStartChar = char pos of
var destinationLabel
function whereIsSelection()
{
if((selectionStartChar > myCharPositions[0]) && (selectionStartChar < myCharPositions[1]))
{
alert ("came from the section:" + myLabelsArray[0] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[0]
}
if((selectionStartChar > myCharPositions[1]) && (selectionStartChar < myCharPositions[2]))
{
alert ("came from the section:" + myLabelsArray[1] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[1]
}
if((selectionStartChar > myCharPositions[2]) && (selectionStartChar < myCharPositions[3]))
{
alert ("came from the section:" + myLabelsArray[2] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[2]
}
if((selectionStartChar > myCharPositions[3]) && (selectionStartChar < myCharPositions[4]))
{
alert ("came from the section:" + myLabelsArray[3] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[3]
}
if((selectionStartChar > myCharPositions[4]) && (selectionStartChar < myCharPositions[5]))
{
alert ("came from the section:" + myLabelsArray[4] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[4]
}
if((selectionStartChar > myCharPositions[5]) && (selectionStartChar < myCharPositions[6]))
{
alert ("came from the section:" + myLabelsArray[5] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[5]
}
if(selectionStartChar > myCharPositions[6])
{
alert ("came from the section:" + myLabelsArray[6] + " needs to be copied into the same section in the above text area");
destinationLabel = myLabelsArray[6]
}
}

//sixth and final function called
//code to swap selected text from textArea to textArea
var copyStr;
var lbr;
function moveSelection(where)
{
alert("selection b 4 adding section name = " + selectionForMove);
alert("destination section = " + destinationLabel);
var finalStringForMove = (destinationLabel + selectionForMove);
alert ("Our final string to move = " + finalStringForMove);
lbr='';
if (document.selection)
{
tmp=where.value;
sel = document.selection.createRange();
copyStr=sel.text;
document.selection.clear();
tmpx=where.value.replace(/\r\n/g,' ');
tmpy=tmp.replace(/\r\n/g,' ');
tmpz=tmpy.length-tmpx.length-copyStr.length;
for (var i=0;i<tmpz;i++)
{
lbr+='\r\n';
}
}
else if (where.selectionStart || where.selectionStart == '0')
{
var begin = where.selectionStart;
var end = where.selectionEnd;
copyStr=where.value.substring(begin,end);
where.value=where.value.substring(0,begin)+where.value.substring(end,where.value.length)
}
if(copyStr=="")
{
alert('No text selected to be moved!')
}
else
{
document.forms[0].briefString.value+=copyStr+lbr;
}
}


/*
//used for testing mt two global Arrays
//(array) myLabelsArray = actual section labels
//(array) myCharPositions = char locations of sections in forwarded brief(s)
//(var) selectionStartChar = char position at start of users selection (to move)
function testValues()
{
var i = 0
while (i <= 6)
{
alert (myLabelsArray + " is at character " + myCharPositions)
i++;
}
}
*/
//-->
</script>
</head>
<body>
<form>
<textarea name="briefString" rows="13" cols="60">EVENTS:<%= vbcrlf & vbcrlf %>ISSUES:<%= vbcrlf & vbcrlf %>VISITS:<%= vbcrlf & vbcrlf %>REPUTATION:<%= vbcrlf & vbcrlf %>MINISTERIALS:<%= vbcrlf & vbcrlf %>PERSONNAL:<%= vbcrlf & vbcrlf %>ACHIEVEMENTS:</textarea>
<br><br><!--,saveCaret(this);getPosOfStringValues(ta1) --><!---->
<textarea name="ta1" rows="13" cols="60" onSelect="saveCaret(this);">
EVENTS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>
ISSUES:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>
VISITS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>
REPUTATION:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>
MINISTERIALS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>
PERSONNAL:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>
ACHIEVEMENTS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa
</textarea><br><br>
<input name="btn" type="button" onclick="getPosOfStringValues(ta1),GetSelectedTxt(),getCaretPos(ta1),whereIsSelection(),moveSelection(this.form.ta1);" value="Move Selected Text">
<br><br>(document.selection) txt = document.selection.createRange().text;


</form>


</body>
</html>------------------finish code----------------
 
Supra

Please note:
;;append it to the same category in the textarea above. Is this correct?

I was thinking more like appending the category name to the front of my final string and just replace it including the category name. This can be seen on line 112:"alert ("Our final string to move = " + finalStringForMove);"

The variable finalStringForMove already had it appended - am I correct to asume all I have to do now is a find n relace to the top text area? My eyes are hanging out of my head, any help would be appreciated hugely

Thanking you
 
I modified the code somewhat (and eliminated some) so I'm not sure if it's what you wanted, but I took a shot ;)
Code:
<html>

<head>

<title>Untitled</title>

<script language="JavaScript" type="text/javascript">

<!--

//make array global arrays so the elements maybe used in other functions

var myLabelsArray = new Array("EVENTS:","ISSUES:","VISITS:","REPUTATION:","MINISTERIALS:","PERSONNAL:","ACHIEVEMENTS:");

var myCharPositions = new Array(6);



//first function called:get position of section values (Events etc..) values

function getPosOfStringValues(elem)

{

var result;

var i = 0;

while (i <= 6) 

{ 

result = elem.value.search(myLabelsArray[i]);

myCharPositions[i] = result;

//alert (myLabelsArray[i] + " is at character " + myCharPositions[i])

i++; 

}

}



//second function:get selected text

var selectionForMove

function GetSelectedTxt() 

{

range = document.selection.createRange();

selectionForMove = range.text;

//alert("selection = " + selectionForMove);

}



//third function called, run on select of textarea:create range for position 

function saveCaret(elem)

{

if( elem.isTextEdit )

{ 

elem.caretPos = document.selection.createRange();

}

}



//fourth function called:get char position for first char of selection 

var selectionStartChar

function getCaretPos(elem)

{

if ( elem.isTextEdit && elem.caretPos )

{

var orig = elem.value;

var bookmark = "~";

var caretPos = elem.caretPos;

caretPos.text = bookmark;

selectionStartChar = elem.value.search( bookmark );

window.status = "Caret is at character " + selectionStartChar;

//window.document.writeln ("Caret is at character " + selectionStartChar); 

//alert ("Users selection starts at char " + selectionStartChar + " (this is the selection we want to move)" );

elem.value = orig;

}



}



//fifth function called: selectionStartChar = char pos of 

var destinationLabel

function whereIsSelection()

{

if((selectionStartChar > myCharPositions[0]) && (selectionStartChar < myCharPositions[1]))

{

alert ("came from the section:" + myLabelsArray[0] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[0] 

}

if((selectionStartChar > myCharPositions[1]) && (selectionStartChar < myCharPositions[2]))

{

alert ("came from the section:" + myLabelsArray[1] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[1]

}

if((selectionStartChar > myCharPositions[2]) && (selectionStartChar < myCharPositions[3]))

{

alert ("came from the section:" + myLabelsArray[2] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[2]

}

if((selectionStartChar > myCharPositions[3]) && (selectionStartChar < myCharPositions[4]))

{

alert ("came from the section:" + myLabelsArray[3] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[3]

}

if((selectionStartChar > myCharPositions[4]) && (selectionStartChar < myCharPositions[5]))

{

alert ("came from the section:" + myLabelsArray[4] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[4]

}

if((selectionStartChar > myCharPositions[5]) && (selectionStartChar < myCharPositions[6]))

{

alert ("came from the section:" + myLabelsArray[5] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[5]

}

if(selectionStartChar > myCharPositions[6])

{

alert ("came from the section:" + myLabelsArray[6] + " needs to be copied into the same section in the above text area");

destinationLabel = myLabelsArray[6]

}

}



//sixth and final function called

//code to swap selected text from textArea to textArea

var copyStr;

var lbr;

function moveSelection(where)

{

alert("selection b 4 adding section name = " + selectionForMove);

alert("destination section = " + destinationLabel);

var finalStringForMove = (destinationLabel + selectionForMove);

alert ("Our final string to move = " + finalStringForMove);

lbr='';

if (document.selection)

{
orig = where.value;

sCat = orig.indexOf(destinationLabel) + destinationLabel.length;
sRTN = orig.indexOf("\r\n",sCat);
sCat = sRTN;

lCat = orig.substr(0,sCat);
rCat = orig.substr(sCat,orig.length);
document.forms[0].briefString.value = lCat + selectionForMove + rCat;
}

else {
alert("Nothing to move!");

}
}





/*

//used for testing mt two global Arrays

//(array) myLabelsArray = actual section labels 

//(array) myCharPositions = char locations of sections in forwarded brief(s)

//(var) selectionStartChar = char position at start of users selection (to move)

function testValues()

{

var i = 0

while (i <= 6)

{

alert (myLabelsArray[i] + " is at character " + myCharPositions[i])

i++;

}

}

*/

//-->

</script>

</head>

<body>

<form>

<textarea name="briefString" rows="13" cols="60">EVENTS:<%= vbcrlf & vbcrlf %>ISSUES:<%= vbcrlf & vbcrlf %>VISITS:<%= vbcrlf & vbcrlf %>REPUTATION:<%= vbcrlf & vbcrlf %>MINISTERIALS:<%= vbcrlf & vbcrlf %>PERSONNAL:<%= vbcrlf & vbcrlf %>ACHIEVEMENTS:</textarea>

<br><br><!--,saveCaret(this);getPosOfStringValues(ta1) --><!---->

<textarea name="ta1" rows="13" cols="60" onSelect="saveCaret(this);">

EVENTS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>

ISSUES:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>

VISITS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>

REPUTATION:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>

MINISTERIALS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>

PERSONNAL:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa<%= vbcrlf %>

ACHIEVEMENTS:<%= vbcrlf %>aaaaaaaa aaaaaaaaa aaaaaaaa aaaaaaa

</textarea><br><br>

<input name="btn" type="button" onclick="getPosOfStringValues(ta1),GetSelectedTxt(),getCaretPos(ta1),whereIsSelection(),moveSelection(this.form.ta1);" value="Move Selected Text">

<br><br>(document.selection) txt = document.selection.createRange().text;





</form>





</body>

</html>
 
Supra

It seems to move the selected text (into the correct position - very nice!!). however also moves the rest of the text into a mirror image location for where it came from - wow, does that make sense?

Not sure where you are round the world. i'm in Sydney Australia - our office is about to shut for the weekend. I will be onto this later tonight, I appreciate your efforts enormously - now I have my string and the location (section name) I will check out 'find and replace within a textarea' - mate youre a legend!!!!!!!

Thanking you again



 
Not quite sure what you mean. The code above APPENDS the selected text to the correct section in the first textarea. Are you trying to REPLACE the text that was in the first textarea with the user's selection?
 
Supra

The solution - it is now working like I need it with the exception:
It currently cuts the text from the bottom textArea and places it in the upper area into the same section - I need to: copy + click to move + paste (into the same area)

Thanking you

--------------start--cut n paste-----------------
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
//make array global arrays so the elements maybe used in other functions
var myLabelsArray = new Array("EVENTS:","ISSUES:","VISITS:","REPUTATION:","MINISTERIALS:","PERSONNAL:","ACHIEVEMENTS:");
var myCharPositions = new Array(6);
var copyStr;
var lbr;
var foc=0;
var foc2=0;
//first function called:get position of section values (Events etc..) values
function getPosOfStringValues(elem)
{
var result;
var i = 0;
while (i <= 6)
{
result = elem.value.search(myLabelsArray);
myCharPositions = result;
i++;
}
}
function whereIsSelection()
{
mustGoUp="yes";
if((selectionStartChar > myCharPositions[0]) && (selectionStartChar < myCharPositions[1]))
{
insWhere=myLabelsArray[1];
}
if((selectionStartChar > myCharPositions[1]) && (selectionStartChar < myCharPositions[2]))
{
insWhere=myLabelsArray[2];
}
if((selectionStartChar > myCharPositions[2]) && (selectionStartChar < myCharPositions[3]))
{
insWhere=myLabelsArray[3];
}
if((selectionStartChar > myCharPositions[3]) && (selectionStartChar < myCharPositions[4]))
{
insWhere=myLabelsArray[4];
}
if((selectionStartChar > myCharPositions[4]) && (selectionStartChar < myCharPositions[5]))
{
insWhere=myLabelsArray[5];
}
if((selectionStartChar > myCharPositions[5]) && (selectionStartChar < myCharPositions[6]))
{
insWhere=myLabelsArray[6];
}
if(selectionStartChar > myCharPositions[6])
{
insWhere=myLabelsArray[6];
mustGoUp="no";
}
if(document.all)goUp=2;
else goUp=1;
var putIt=document.forms[0].briefString.value;
if(mustGoUp=="yes")//if other than ACHIEVEMENTS, find beginning of next cathegory, go one line up (two chars left in IE, one in Mozilla) and insert selection
{
whereInsert=putIt.indexOf(insWhere);
whereInsert-=goUp;
putIt=putIt.substring(0,whereInsert)+copyStr+lbr+putIt.substring(whereInsert,putIt.length)
}
if(mustGoUp=="no")//if ACHIEVEMENTS, just add selection to complete value
{
putIt+=copyStr+lbr
}
document.forms[0].briefString.value=putIt;
}
function moveSelection(where,whofoc)
{
if(whofoc==0)
{
alert('The source textarea does not have focus and thus, no selection!');
return false;
}
lbr='';
if (document.selection)
{
tmp=where.value;
sel = document.selection.createRange();
copyStr=sel.text;
if(copyStr=="")
{
alert('No text selected to be moved!')
return false;
}
sel.text="~"+sel.text
selectionStartChar = where.value.indexOf("~");
document.selection.clear();
tmpx=where.value.replace(/rn/g,' ');
tmpy=tmp.replace(/rn/g,' ');
tmpz=tmpy.length-tmpx.length-copyStr.length;
for (var i=0;i<tmpz;i++)
{
lbr+='\r\n';
}
}
else if (where.selectionStart || where.selectionStart == '0')
{
var begin = selectionStartChar=where.selectionStart;
var end = where.selectionEnd;
copyStr=where.value.substring(begin,end);
where.value=where.value.substring(0,begin)+where.value.substring(end,where.value.length)
}
if(copyStr=="")
{
alert('No text selected to be moved!')
}
else
{
whereIsSelection();
}
}
//-->
</script>
</head>
<body>
<form>
<textarea name="briefString" rows="13" cols="60">EVENTS:

ISSUES:

VISITS:

REPUTATION:

MINISTERIALS:

PERSONNAL:

ACHIEVEMENTS:
</textarea>
<br><br>
<textarea name="ta1" rows="13" cols="60" onfocus="foc=1" onblur="foc=0">
EVENTS:
eeeee eeeee eeeeee eeeeee
ISSUES:
iiiii iiiii iiiii iiiiii
VISITS:
vvvvv vvvvv vvvvv vvvvv
REPUTATION:
rrrrr rrrrrr rrrrr rrrrrr
MINISTERIALS:
mmmmm mmmmm mmmmm mmmmm
PERSONNAL:
ppppp ppppp ppppp ppppp
ACHIEVEMENTS:
aaaaa aaaaa aaaaa aaaaa
</textarea><br><br>
<input name="btn" type="button" onclick=" getPosOfStringValues(ta1),moveSelection(this.form.ta1,foc2);" onmousedown="foc2=foc;" value="Move Selected Text">
<br><br>
</form>
ljknlknlklnk
</body>
</html>
----------------------finish-------------------
 
I reset the value of the 2nd textarea (ta1) to what it was before the tilde (~) was added. I also removed document.selection.clear() as it was not needed in my example. Works ok for me :)
Code:
function moveSelection(where,whofoc) 
{ 
if(whofoc==0) 
{ 
alert('The source textarea does not have focus and thus, no selection!'); 
return false; 
} 
lbr=''; 
if (document.selection) 
{
tmp=where.value; 
sel = document.selection.createRange(); 
copyStr=sel.text;
if(copyStr=="") 
{ 
alert('No text selected to be moved!') 
return false; 
} 
sel.text="~"+sel.text;
selectionStartChar = where.value.indexOf("~"); 
tmpx=where.value.replace(/rn/g,' '); 
tmpy=tmp.replace(/rn/g,' '); 
tmpz=tmpy.length-tmpx.length-copyStr.length; 
for (var i=0;i<tmpz;i++) 
{ 
lbr+='\r\n'; 
} 
[green]document.forms[0].ta1.value = tmp;[/green]
} 
else if (where.selectionStart || where.selectionStart == '0') 
{ 
var begin = selectionStartChar=where.selectionStart; 
var end = where.selectionEnd; 
copyStr=where.value.substring(begin,end); 
where.value=where.value.substring(0,begin)+where.value.substring(end,where.value.length) 
} 
if(copyStr=="") 
{ 
alert('No text selected to be moved!') 
} 
else 
{ 
whereIsSelection(); 
} 
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top