Hi,
I have this function, which is a forum function. It surrounds the highlighted text when a button is clicked with coding. Such as where you type messages in the forum, you highlight the text you want in bold, and click the B icon and it puts the bold around the text.
[CODE]
// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
// Can a text range be created?
if (typeof(textarea.caretPos) != “undefined” && textarea.createTextRange)
{
var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
caretPos.text = caretPos.text.charAt(caretPos.text.length – 1) == ‘ ‘ ? text1 + caretPos.text + text2 + ‘ ‘ : text1 + caretPos.text + text2;
if (temp_length == 0)
{
caretPos.moveStart(“character”, -text2.length);
caretPos.moveEnd(“character”, -text2.length);
caretPos.select();
}
else
textarea.focus(caretPos);
}
// Mozilla text range wrap.
else if (typeof(textarea.selectionStart) != “undefined”)
{
var begin = textarea.value.substr(0, textarea.selectionStart);
var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd – textarea.selectionStart);
var end = textarea.value.substr(textarea.selectionEnd);
var newCursorPos = textarea.selectionStart;
var scrollPos = textarea.scrollTop;
textarea.value = begin + text1 + selection + text2 + end;
if (textarea.setSelectionRange)
{
if (selection.length == 0)
textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
else
textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
textarea.focus();
}
textarea.scrollTop = scrollPos;
}
// Just put them on the end, then.
else
{
textarea.value += text1 + text2;
textarea.focus(textarea.value.length – 1);
}
}
And here is an example of the calling function:
[CODE]
<a STYLE=”text-decoration:none” href=”javascript:void(0);” onclick=”surroundText(‘Δ’ ,” , document.form0.definition0); return false;”>Δ</a>
Now, my only problem is, in this webpage I have multipule text areas I want the same button to work for all of them. But the calling function specifies (and the function requires) a specific textare name. However, each textarea has a different name.
Is there anyway I can make this function work (or another function), so the button to click to add the coding in front of and behind the text works for multiple text areas on one webpage?