Qui hai tutto quello che ti serve per modificare il mio esempio...
https://developer.mozilla.org/en/DOM...extAreaElement
P.S. Ti ricordo che Google è vivo e lotta insieme a noi... la prossima volta consultalo!
codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="it" />
<title>Esempio per il Forum di HTML.it</title>
<script type="text/javascript">
function nuovoTag(sStartTag1, sEndTag1) {
var nNewCursorPos = document.miaForm.miaTA.value.length + sStartTag1.length;
document.miaForm.miaTA.value += sStartTag1 + sEndTag1;
document.miaForm.miaTA.selectionStart = document.miaForm.miaTA.selectionEnd = nNewCursorPos;
document.miaForm.miaTA.focus();
}
function racchiudiTesto(sStartTag2, sEndTag2) {
var nSelStart = document.miaForm.miaTA.selectionStart, nSelEnd = document.miaForm.miaTA.selectionEnd;
if (nSelStart === nSelEnd) { alert("Devi selezionare una porzione di testo!!"); return; }
var sOldText = document.miaForm.miaTA.value;
document.miaForm.miaTA.value = sOldText.substring(0, nSelStart) + sStartTag2 + sOldText.substring(nSelStart, nSelEnd) + sEndTag2 + sOldText.substring(nSelEnd, sOldText.length);
document.miaForm.miaTA.setSelectionRange(nSelStart + sStartTag2.length, nSelEnd + sStartTag2.length);
document.miaForm.miaTA.focus();
}
</script>
<style type="text/css">
.intLink {
cursor: pointer;
text-decoration: underline;
color: #0000ff;
}
</style>
</head>
<body>
<form name="miaForm">
[&nbsp;<span class="intLink" onclick="nuovoTag('[TAG1]','[/TAG1]');">Nuovo tag</span> | <span class="intLink" onclick="racchiudiTesto('[TAG2]','[/TAG2]');">Racchiudi testo selezionato in un tag</span>&nbsp;]</p>
<textarea rows="10" cols="50" name="miaTA">Testo di esempio</textarea></p>
</form>
</body>
</html>