|
I have a js function that make selected portion of text in a textarea bold.
and I have 2 textareas named 'frst' and 'scnd' consequently and I have one button called 'Make it Bold' which calls my above function to formate the text selected in textarea "frst" as bold text.
To call my function I need to send the textarea name (which i want its text to be formatted as the following: (Notice I send the name of the first textarea which is 'frst')
input name="Button" type="button" onclick="fBold(this.form.frst;" value="Make it Bold"/ |
|
My problem is that I want to send the textarea name as a variable instead of sending a specific texatrae name as I have 2 and some times more than 2 textarea on same page, thus I want the name of the textarea which has cursor focused on it to be dynamically sent when pressing the 'Make it bold' button
Summary: I need to know the name of the textarea which currently has the cursor set inside and then send this name to the button of 'Make it bold' which then will call my function that makes text bold.
i.e I want it look like:
<input name="Button" type="button" onclick="fBold(this.form.TEXTAREA NAME;" value="Make it Bold"/>
|
|
where TEXTAREA NAME is the name of the textarea which currently has cursor set on.
My code is :
<script type="text/javascript">
// the 'storeCaret' function is concerned with knowing the postion the cursor is currently standing at
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
// the fbold is concerned with giving effects to the text
function fBold (textEl)
{
var range=document.selection.createRange();
if (textEl.createTextRange && textEl.caretPos)
{
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : ("<b>"+range.text+"</b>");
}
}
</script>
<textarea name = 'frst'
onselect="storeCaret(this);"
onclick="storeCaret(this);"
onkeyup="storeCaret(this);"
ondblclick="storeCaret(this);"
>This is first textarea</textarea>
<input name="button" type="button" onclick="fBold(this.form.frst);" value="Make it Bold"/>
|
|
I hope I have explained my self well
Thanks for you help
|
|
|
|
|
|
|
|