faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

95 of 158 people (60%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I insert text in front of and after selected area in textarea

Aug 27th, 2007 13:39
John Doe, John F, wicked bob, Tom Kopke, Peter Tilsted,


This concantenates field values into one value.
<script LANGUAGE="JavaScript">
<!--
function addtext(form) {
name1 = (form.before.value + form.name1.value + form.after.value)
}
//-->
</SCRIPT>
In your form add the before and after text you want like the below:
<input TYPE="hidden" NAME="before" VALUE="before text">
<input TYPE="hidden" NAME="after" VALUE="after text">
Of course, you'll need the text area too:
<textarea NAME="name1">
Execute function "addtext" by your favorite method such as onclick. 
_____________________________________________
Well call me blonde, but i didn't have much luck implementing the above 
code and i found something else that worked first time.
Enter the following in the <head> of your document:
function FUNCTIONNAME(from) { 
strSelection = document.selection.createRange().text 
if (strSelection == "") { 
return false; 
} 
else document.selection.createRange().text = "LEFT ENTRY" + 
strSelection + "RIGHT ENTRY"
return;
} 
Then you can invoke this function anywhere in the <body> of your 
document with:
onclick="FUNCTIONNAME()"
It is as simple as that.
NB: FUNCTIONNAME, LEFT ENTRY and RIGHT ENTRY can be whatever you like, 
obviously.  Also note, that 'return false;' referenced whenever 
strSelection == "" can be replaced with any other appropriate action 
you prefer, if you want more than nothing to happen when your function 
is invoked but no selection has actually been made.
The following example places Bold tags before and after selected text 
in a textarea:
<html>
<head>
<title>Javascript Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" language="javascript"> function
boldThis(from) { 
strSelection = document.selection.createRange().text 
if (strSelection == "") { 
return false; 
} 
else document.selection.createRange().text = "<b>" + strSelection 
+ "</b>" 
return;
}
</script>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<form name="form1" method="post" action="">
<textarea name="textarea" cols="50" rows="20"></textarea><br>
<input type="button" value="Bold" onclick="boldThis()">
</form>
</body>
</html>
______________________________________________
I believe the first answer might actually be useful for 
adding text(s) before and after the entire contents of a
textarea. "Selected" text has no effect.
The second answer works only for IE. Netscape doesn't offer
this feature. See 
http://www.faqts.com/knowledge_base/view.phtml/aid/3194/fid/130
______________________________________________
Actually, netscape(firefox) offers this feature.
-> http://www.faqts.com/knowledge_base/view.phtml/aid/13502/fid/130