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!

textarea to textarea (selected values only)

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
Hello JS gurus

I have a tricky litle task on my hands, forgive my previous post - it was in correct.

I have three dynamicly populated textareas, each having thier own 'moveText' link associated, plus one empty textArea. My objective is to let the user select text from a text area then click the button to move the selected text to the empty textarea. Can this be done??

Further breakdown: How could I detect what values have been selected in a text area, is there a function for this??

Once I have these values it seems fairly strait forward to populate the empty area

A bit stuck, thank you in advance
 
hi,
i read about this function createTextRange() in this forum, try earching for its use on the web, it may help...

Known is handfull, Unknown is worldfull
 
Something like this?
Code:
<script>
<!--
function feedHim() {
	food = document.selection.createRange();
	alert("You selected: " + food.text);
	t2.value = food.text;
}
//-->
</script>

<textarea id="t1">I'm the text monster.  Feed me some text.</textarea>
<br>
<textarea id="t2"></textarea>
<br>
<input type="button" value="Feed Me" onClick="feedHim()">
 
vbkris

Allot of progress has been made (90% complete) since my last post, the following is cut n paste code and has notes intended to be read once the page has been run. It contains some .asp code (only used for formatting within the textareas. Formatting will be lost but it will run and work as a .html page)

I am a bit stuck getting the remaining functionality to work (not to techo with js), createRange - yes this was the function I needed. It works well

Would appreciate any input

-----------------------------------start--------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var copyStr;
var foc=0;
var foc2=0;
var lbr;
function moveSelection(where,btn,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;
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;
}
}
//-->
</script>
</head>
<body>
<!--the following notes are intended to be read once the page has been run in the browser -->
This pages objective is 90% finished. There are categories in each of
the four text areas, Eg EVENTS:etc... The bottom three areas will
contain text against each section. At this point and time, if you
select text form one of the bottom three areas and click 'move selected'
the text is moved to the top textArea(appended to the bottom) - nice! I am trying to:<br>
a..detect which section the selected text came from<br>
b..move this text in to this category in the top textArea<br>
Am I dreaming?<bR>
(BTW: if you have copied this page as a .html ext you will not see the correct 'textArea content' formatting - however it will still work)
<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>
<textarea name="ta1" rows="5" cols="60" onfocus="foc=1" onblur="foc=0">
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>
<input name="btn" type="button" onmousedown="foc2=foc;" onmouseup="moveSelection(this.form.ta1,this,foc2)" value="move selected">
<br><br>

<textarea name="ta2" rows="5" cols="60" onfocus="foc=1" onblur="foc=0">
EVENTS:<%= vbcrlf %>bbbbbbbbb bbbbbbbbbb bbbbbbbbbb bbbb<%= vbcrlf %>
ISSUES:<%= vbcrlf %>bbbbbbbb b bbbbbbbb bbbbbbbb bbbbbb<%= vbcrlf %>
VISITS:<%= vbcrlf %>bbbbbbbb b bbbbbbbb bbbbbbbb bbbbbb<%= vbcrlf %>
REPUTATION:<%= vbcrlf %>bbbbbbbb b bbbbbbbb bbbbbbbb bbbbbb<%= vbcrlf %>
MINISTERIALS:<%= vbcrlf %>bbbbbbbb b bbbbbbbb bbbbbbbb bbbbbb<%= vbcrlf %>
PERSONNAL:<%= vbcrlf %>bbbbbbbb b bbbbbbbb bbbbbbbb bbbbbb<%= vbcrlf %>
ACHIEVEMENTS:<%= vbcrlf %>bbbbbbbb b bbbbbbbb bbbbbbbb bbbbbb
</textarea><br>
<input name="btn" type="button" onmousedown="foc2=foc;" onmouseup="moveSelection(this.form.ta2,this,foc2)" value="move selected">
<br><br>

<textarea name="ta3" rows="5" cols="60" onfocus="foc=1" onblur="foc=0">
EVENTS:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc<%= vbcrlf %>
ISSUES:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc<%= vbcrlf %>
VISITS:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc<%= vbcrlf %>
REPUTATION:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc<%= vbcrlf %>
MINISTERIALS:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc<%= vbcrlf %>
PERSONNAL:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc<%= vbcrlf %>
ACHIEVEMENTS:<%= vbcrlf %>ccccc ccccccccc cccccccc ccccccc ccc
</textarea><br>
<input name="btn" type="button" onmousedown="foc2=foc;" onmouseup="moveSelection(this.form.ta3,this,foc2)" value="move selected">
<br><br>

</form>
</body>
</html>
-----------------------------------finish--------------------------------
 
So like this..
Code:
<script>
<!--
function feedHim() {
	food = document.selection.createRange();
		if (food.text) {
			alert(srcBox + " offers the textarea some food.");
			tDest.value = food.text;
		}
}
//-->
</script>

<textarea id="tSrc1" onselectstart="srcBox=this.id">I'm the first box.</textarea>
<textarea id="tSrc2" onselectstart="srcBox=this.id">I'm the second box.</textarea>
<textarea id="tSrc3" onselectstart="srcBox=this.id">I'm the third box.</textarea>
<br>
<textarea id="tDest">Feed me</textarea>
<br>
<input type="button" value="Feed Me" onClick="feedHim()" id="butt">
 
Oops.. you should make srcBox global:
Code:
<script>
<!--
var srcBox;

function feedHim() {
    food = document.selection.createRange();
        if (food.text) {
            alert(srcBox + " offers the textarea some food.");
            tDest.value = food.text;
        }
}
//-->
</script>
 
Supra

Thank you for your time, yes sort of.

Did you run the cut n paste code? sorry bout the visual disarray - it becomes very clear if you run it in a browser - preferably as a .asp page (to hold the 'inside text area' formatting of my sections)

I am looking at inserting the selection in a certain section of the textArea (inserting to the same section as its original location - it all becomes clear when you run the page)

Thanking you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top