totallymad: problem mit bbcode

ich habe mit php einen kleinen bbcode geschrieben und um den einsatz leichter zu machen habe ich ein kleines js geschrieben

function bbcode(x) {
y=document.selection.createRange().text;
n=window.prompt("Text der formatiert geschrieben werden soll:",y);
if ((n != null) && (n != "")) {
document.selection.clear();
a=document.gb.text.value;
document.gb.text.value=a+"["+x+"]"+n+"[/"+x+"]";
}
}

aufgerufen wird es durch
<a href=javascript:bbcode('b')>=</a> <b>fett</b>

um nachträgliches formatieren einfacher zu machen habe ich versucht das der makierte text automatisch in das window.prompt übernommen wird
nur das problem ist das wenn man einen textteil aus der mitte formatieren will wird dieser dort gelöscht und ans ende des textes eingefügt

kann mir jemand helfen wie ich es schaffe das der text an der selbe stelle bleibt?
mfg herwig

  1. Hallo,

    hatte neulich auch so ein Problem. Hier eine Lösung: http://forum.de.selfhtml.org/archiv/2003/5/45588/#m250095

    Grüße, Alex

    1. function bbcode(x) {
      y=document.selection.createRange().text;
      n=window.prompt("Text der formatiert geschrieben werden soll:",y);
      if ((n != null) && (n != "")) {
      document.selection.clear();
      a=document.gb.text.value;
      input('[' + x + ']'+y+'[/' + x + '] ');
      }
      }

      function input(what)
       {
        if (document.gb.text.createTextRange)
         {
          document.gb.text.focus();
          document.selection.createRange().duplicate().text = what;
         }
        else if (document.getElementById && !document.all) // Mozilla
         {
          var tarea = document.forms['gb'].elements['text'];
          var selEnd = tarea.selectionEnd;
          var txtLen = tarea.value.length;
          var txtbefore = tarea.value.substring(0,selEnd);
          var txtafter =  tarea.value.substring(selEnd, txtLen);
          tarea.value = txtbefore + what + txtafter;
         }
        else
         {
          document.entryform.text.value += what;
         }
       }

      thx die funktion input hat mir wirklich sehr geholfen mit dem script klappts jetzt wie ich ma des vorstell ;)