Entry
Can I insert a tag around a highlighted piece of text in a textarea with a button?
Jun 13th, 2002 07:08
Jon Cage, Fredrik Sjökvist,
Here's a little script that puts tags around a bit of text :
function addSinglePartTag(tagName, prompt, defaultText)
{
// If something is selected then just encase it in the tags
var selection = document.selection.createRange().text;
if(selection != "")
{
self.formContainingTextArea.theTextArea.focus();
var newSelection = document.selection.createRange();
newSelection.text
= '['+tagName+']'+selection+'[/'+tagName+']';
return;
}
// Nothing's selected so prompt for something to put between the tags
var newTag = self.prompt(prompt, defaultText);
if((newTag == null) || (newTag == defaultText))
return;
newTag='['+tagName+']'+stripSpaces(newTag)+'[/'+tagName+']';
self.formContainingTextArea.theTextArea += newTag;
}
Just create a button in your document and voila :
<INPUT TYPE="button" class="button" VALUE="Add URL (Plain)"
onClick="addSinglePartTag('URL', 'Please enter a valid
URL :', 'http://'); formContainingTextArea.theTextArea.focus(); return
true;">