Good afternoon,
first time using this website so please allow some slack if I come accross to forward/stupid!
Ok, i've been migrating certain scripts into a functional textarea edit box. The script runs on a messageboard service where they have special charictures which represent differnt html code (for instance b[]b replaces <b></b>) Much the same as this board!
What i've done is create some javascript (with appropriate buttons calling the function) to insert these codes. However what I ideally want as well, is if somebody select some text in the textarea and clicks the button is to surround the selected text with the special charictures.
The code which i've got is below:
first time using this website so please allow some slack if I come accross to forward/stupid!
Ok, i've been migrating certain scripts into a functional textarea edit box. The script runs on a messageboard service where they have special charictures which represent differnt html code (for instance b[]b replaces <b></b>) Much the same as this board!
What i've done is create some javascript (with appropriate buttons calling the function) to insert these codes. However what I ideally want as well, is if somebody select some text in the textarea and clicks the button is to surround the selected text with the special charictures.
The code which i've got is below:
Code:
<SCRIPT language=JavaScript>
function insert(codeToInsert)
{
sCbCode= codeToInsert;
insertAtCaret (document.postform.message, sCbCode, 2)
}
function storeCaret(objtextarea)
{
if (objtextarea.createTextRange)
{
objtextarea.caretPos = document.selection.createRange().duplicate();
}
}
function insertAtCaret(objtextarea, stext,nplacement)
{
if (objtextarea.createTextRange && objtextarea.caretPos)
{
var caretPos = objtextarea.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? stext + ' ' : stext;
}
else {
objtextarea.value = stext;
}
objtextarea.focus();
}
</SCRIPT>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<a href="#" onClick="insert('b[ ]b')">Bold</a> |
<a href="#" onClick="insert('i[ ]i')">Italics</a> |
<a href="#" onClick="insert('u[ ]u')">Underline</a> |
<a href="#" onClick="insert('+[ ]+')">Bullet Points</a> |
<a href="#" onClick="insert('q[ ]q')">Quoted Text</a> |
<a href="#" onClick="insert('img[ ]img')">Insert an image</a>
</td>
</table>
<form name=postform>
<textarea name="message" cols=40 rows=5 class="input" ONSELECT="storeCaret(this);" ONCLICK="storeCaret(this);" ONKEYUP="storeCaret(this);" >
</form>