in realtà stavo pensando che quello che ti potrebbe servire sia qualcosa di più semplice ma più funzionale...
[CODE]
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="it" />
<title>Esempio per il Forum di HTML.it</title>
<script type="text/javascript">
function surroundText(sStartTag, sEndTag) {
var nSelStart = document.myForm.myTxtArea.selectionStart, nSelEnd = document.myForm.myTxtArea.selectionEnd, sOldText = document.myForm.myTxtArea.value;
document.myForm.myTxtArea.value = sOldText.substring(0, nSelStart) + sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag + sOldText.substr(nSelEnd);
document.myForm.myTxtArea.setSelectionRange(nSelSt art + sStartTag.length, nSelEnd + sStartTag.length);
document.myForm.myTxtArea.focus();
}
function replaceText(sNewText) {
var nSelStart = document.myForm.myTxtArea.selectionStart, nSelEnd = document.myForm.myTxtArea.selectionEnd, sOldText = document.myForm.myTxtArea.value;
document.myForm.myTxtArea.value = sOldText.substring(0, nSelStart) + sNewText + sOldText.substr(nSelEnd);
document.myForm.myTxtArea.setSelectionRange(nSelSt art === nSelEnd ? nSelStart + sNewText.length : nSelStart, nSelStart + sNewText.length);
document.myForm.myTxtArea.focus();
}
</script>
<style type="text/css">
.intLink {
cursor: pointer;
text-decoration: underline;
color: #0000ff;
}
</style>
</head>
<body>
Quest'esempio funziona sia che ci sia del testo selezionato nella textarea sia che non ci sia.</p>
<form name="myForm">
[&nbsp;<span class="intLink" onclick="surroundText('[B]','[\/B]');">grassetto</span> | <span class="intLink" onclick="surroundText('[I]','[\/I]');">corsivo</span> | <span class="intLink" onclick="surroundText('[URL='+prompt('Enter the full URL for the link')+']','[\/URL]');">collegamento</span> | <span class="intLink" onclick="surroundText('\n@Xinod e @br1codice:\n','\n[\/CODE]\n');">codice</span> | <span class="intLink" onclick="replaceText(' :-)');">smile</span> | etc. etc.&nbsp;]</p> <textarea rows="10" cols="50" name="myTxtArea">Testo di esempio</textarea></p> </form> </body> </html>
Date uno sguardo a queste due semplici funzioni... credo sarebbe una buona idea utilizzarle per il form delle risposte di questo forum. Potrebbe sembrare una cosa da poco, ma avere il cursore posizionato al posto giusto dopo l'inserimento di un tag o di uno smile non è cosa da buttar via... Che ne pensate?

Rispondi quotando