Let me first start out by saying, I officially SUCK at Javascript. I have in fact searched this Forum and many others looking for exactly what I need.
I am working on a small project, that, like this forum uses some Custom HTML Tags. It appears that many people want to implement a TextArea box with buttons that insert tags in a specific cursor location.
I found the code listed here:
[URL=http://www.webdeveloper.com/forum/showthread.php?t=72414]http://www.webdeveloper.com/forum/showthread.php?t=72414
The Code is as Follows:
[code]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<HTML lang=”en”>
<HEAD>
<TITLE>Text Editor</title>
<META http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<META name=”Content-Script-Type” content=”text/javascript”>
<SCRIPT type=”text/javascript”>
<!–
function insertCode(){
var subject=document.forms.myForm.myTextarea;
if (document.selection) {
var range=document.selection.createRange();
if (range.parentElement()==subject) {
range.text=arguments[0]+(arguments[1]?range.text+arguments[1]:””);
return;
}
}
else if (subject.selectionStart || subject.selectionStart==’0′) {
var str=subject.value;
var a=subject.selectionStart, b=subject.selectionEnd;
subject.value=str.substring(0,a)+arguments[0]+(arguments[1]?str.substring(a,b)+arguments[1]:””)+str.substring(b, str.length);
return;
}
subject.value+=arguments[0]+(arguments[1] || “”);
};
//–>
</SCRIPT>
</HEAD>
<BODY>
<FORM name=”myForm”>
<TEXTAREA name=”myTextarea” cols=”32″ rows=”7″>aaa</TEXTAREA><BR>
<INPUT type=”button” value=”Bold” onclick=”insertCode(‘[B]’, ‘[/B]’);”>
<INPUT type=”button” value=”Smiley” onclick=”insertCode(‘:)’);”>
</BODY>
</HTML>
Now, to my question. I have tried for about 5 hours to make it so that I use ONE Function (InsertCode) and pass it a form control to ultimately return the text to.
Reason is, I have 5 different TextAreas, with 5 different buttons for each. I don’t want to write 25 functions if I don’t have to. So I figured there HAS to be a way to include the TextArea you want it to return to in the onClick portion. All my tests have failed. But as I said before, I SUCK at Javascript.
I am assuming it’s possible, and I apologize if I was not able to find an answer that was looking right at me, but I did put a lot of effort into this before posting this question.
Any help would be greatly appreciated…
-Brad