views:

28

answers:

1

I'm searching for a jQuery plugin or function to insert bb code in a selected textarea - nothing more. I found plugins like http://markitup.jaysalvat.com/downloads/ but they add are to oversized for my project.

All i want is the following function realised in jQuery:

document.getElementById("bold_button").onclick = function() {
   format("[b]", "[/b]");
}


function format(aTag, eTag) {
 var input = document.getElementById('t_antwort'); 
 input.focus();

 if(typeof document.selection != 'undefined') {
  var range = document.selection.createRange();
  var insText = range.text;
  range.text = aTag + insText + eTag;
  /* Anpassen der Cursorposition */
  range = document.selection.createRange();
  if (insText.length == 0) {
    range.move('character', -eTag.length);
  } else {
    range.moveStart('character', aTag.length + insText.length + eTag.length);      
  }
  range.select();
   }
   /* für neuere auf Gecko basierende Browser */
   else if(typeof input.selectionStart != 'undefined')
   {
  /* Einfügen des Formatierungscodes */
  var start = input.selectionStart;
  var end = input.selectionEnd;
  var insText = input.value.substring(start, end);
  input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
  /* Anpassen der Cursorposition */
  var pos;
  if (insText.length == 0) {
    pos = start + aTag.length;
  } else {
    pos = start + aTag.length + insText.length + eTag.length;
  }
  input.selectionStart = pos;
  input.selectionEnd = pos;
   }
   /* für die übrigen Browser */
   else
   {
  input.innerHTML += aTag;
   }

}

Do you know a plugin or function like that?

A: 

This script is very old, but it's still used in many websites (although I do not know whom to particularly credit for it). You need simpletag function.

Something like this should work:

<input type='button' value='B' onclick='simpletag("B");' name='B' /> 
Nikita Rybak