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 Formatting.

Status
Not open for further replies.

Deltaflyer

Programmer
Oct 11, 2000
184
GB
I want to be able to add HTML tags to a textarea selection, but dont know how to access selected text.

I was thinking something like
document.form.field.value.selection = &quot;<B>&quot; + document.form.field.value.selection + &quot;</B>&quot;

Can anyone help me out, how do i access the selected text in a textarea.

Thanks for your assistance, DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
hie
smth like this?
thread216-112980 regards, vic
 
Thanks for the link, i didnt see that one while i was looking. It works with one slight bug.

This is a simulation of the page i want to integrate this functionality into, the problem i have got is, i only want to add tags to TEXTAREA 1. However if you select text in TEXTAREA 2 and click the B link, it is modifying TEXTAREA 1.

How can i check where the focus is i.e. if (!document.frm.txt1.focus){.....} or is there anything else i can do to make this work??

Code:
<HTML>
<HEAD>
<SCRIPT language=&quot;JavaScript&quot;>
function format(wholetext)
    {
	wholetext=document.frm.txt.value
    sel = document.selection;
    rng = sel.createRange();
	alert(sel + '\n\n' + rng)
	if (rng.text.length != 0)
	   {
	   var pretxt = wholetext.slice(0,wholetext.indexOf(rng.text))
	   var modtxt = rng.text
	   var psttxt = wholetext.slice(wholetext.indexOf(rng.text)+rng.text.length,wholetext.length)
    
	   document.frm.txt.value = pretxt
	   document.frm.txt.value = document.frm.txt.value + &quot;<b>&quot; 
	   document.frm.txt.value = document.frm.txt.value + modtxt
	   document.frm.txt.value = document.frm.txt.value + &quot;</b>&quot;
	   document.frm.txt.value = document.frm.txt.value + psttxt
	   document.selection.empty()
	   document.frm.txt.value = document.frm.txt.value + &quot;&quot;
	   }
	}
</SCRIPT>
</HEAD>
<BODY>
<form name=frm>
TextArea 1 <textarea rows=5 cols=50 name=txt>abc def ghi jkl mno pqr stu vwx yz</textarea><br>
TextArea 2 <textarea rows=5 cols=50 name=txt1>zy xwv uts rqp onm lkj ihg fed cba</textarea>
<a href=javascript:format()>B</a>
</form>
</BODY>
</HTML>

Thanks for all help so far, DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
>>How can i check where the focus is
thread216-104533
in ur script u hardcode ur change (i.e. document.frm.txt.value + &quot;<b>&quot;) - u shuldnt.. regards, vic
 
Is there no other way than coding each field for something so small.

I have about 200 fields on this page, i don't wanna have to code onfocus to all of them if i don't have to.

Can't i just ask it which field its looking at? DeltaFlyer
The Only Programmer To Crash With Style. LOL
 
hi, me again
i'm afraid there is no other way.. :-( at least we were discussin this subject here (u saw this thread) & we havent found a solution that wuld be better.. i also asked my friends - looks like nobody knows.. it must be smth very simple or smth very dfclt
but
u're able 2 write a function that wuld go thru all ur fields & set this onfocus handler.. kno what i mean?
regards, vic
 
Hello,
I would recommend using onselect event for necessary textarea.
<textarea rows=5 cols=50 name=txt onselect=&quot;format()&quot;>abc def ghi jkl mno pqr stu vwx yz</textarea><br>
So it will not fire if selecetion happens in other textarea.
D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top