/    Sign up×
Community /Pin to ProfileBookmark

Surround Text Function work multiple textareas

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);
}
}

[/CODE]

And here is an example of the calling function:

[CODE]
<a STYLE=”text-decoration:none” href=”javascript:void(0);” onclick=”surroundText(‘&Delta;’ ,” , document.form0.definition0); return false;”>&Delta;</a>
[/CODE]

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?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 19.2008 — <i>
</i>onclick="surroundText('&amp;Delta;' ,'' , document.[[COLOR="Blue"]'formname'[/COLOR]][[COLOR="Blue"]'textareaname'[/COLOR]]); return false;"
Copy linkTweet thisAlerts:
@enahsauthorMay 19.2008 — Hi,

That does not work (in FireFox or I.E.). And it stops it from working in the hard-coded field as well (i.e. the one that was already working).

I even added a . between the two ][.

No joy.
Copy linkTweet thisAlerts:
@KorMay 20.2008 — Should work... Anyway, try DOM syntax:
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
/*&lt;![CDATA[*/
function surroundText(text1, text2, textarea){
alert(text1);
alert(text2);
alert(textarea.value);
}
/*]]&gt;*/
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=""&gt;
&lt;textarea name="myTextarea"&gt;&lt;/textarea&gt;
&lt;/form&gt;
&lt;div&gt;
&lt;a href="#" onclick="surroundText('&amp;Delta;','',document.getElementsByName('myTextarea')[0]);return false"&gt;&amp;Delta;&lt;/a&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@enahsauthorMay 20.2008 — Hi.

That does not work either.

The Surround Text function is one like from this forum (does the same thing), but not it exactly. Could that be the problem?
Copy linkTweet thisAlerts:
@enahsauthorMay 21.2008 — Hello again,

I looked back over your code. It does nothing for me. You are just specifying a from name in the DOM getElements function. Which each form has a different name, so it will not work for multiple forms on a page.
×

Success!

Help @enahs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...