Hi all,

After over a month of pounding my head against the wall (and getting ZERO help from the TinyMCE "support" forum), I have finally come up with a simple JS function (for the TinyMCE editor) that will wrap a selection with any open and any close tag one wishes, and keeps the selected text selected for further wrapping if desired.

Here's the code (commented heavily):

Code:
/* -------- FOR TINYMCE ONLY --------
* wrap selection with BBCode or other tags
* (c) 2010 r.a.krupski <krupski@roadrunner.com>
* free software - use, modify, distribute freely
** -------- FOR TINYMCE ONLY --------*/
function tinyWrap(open_tag, close_tag)
{
    var ed = (tinymce.activeEditor || opener.tinymce.activeEditor); /* get editor instance */
    var selection = ed.selection.getContent(); /* get user selection, if any */
    var temp_name  = new Date().getTime().toString(36).toLowerCase(); /* generate a unique string */
    var span_open  = '<span id="' + temp_name + '">'; /* generate '<span id="unique">' */
    var span_close = '</span>'; /* generate closing '</span>' */
    selection = selection.replace(/<p>/g,'').replace(/<\/p>/g,'<br />'); /* convert <p></p> to <br /> */
    if (selection.substr(selection.length-6) === '<br />')
    {
        selection = selection.substr(0, selection.length-6); /* strip last <br /> if present */
    }
    /* create complete selection replacement: '<span id="unique">OPEN_TAGselectionCLOSE_TAG</span>' */
    var content = open_tag + span_open + selection + span_close + close_tag;
    ed.execCommand('mceReplaceContent', false, content); /* replace selection with new */
    var span_elem = ed.dom.get(temp_name); /* get the element of the temp span */
    ed.selection.select(span_elem); /* select (highlight) the selection span */
    ed.dom.remove(temp_name, true); /* remove the span, leave it's highlighted text behind(true) */
    ed.focus(); /* insure editor has focus */
    return; /* taa-daa! all done! */
}
Hope this helps someone - I've got a few more gray hairs because of TinyMCE... hopefully this will spare others my agony......

-- Roger