Skip to content Skip to sidebar Skip to footer

Adding Tags To Selection

I am using this Javascript to effect the following changes in my selected text: function formatText(el,tag){ var selectedText = document.selection?document.selection.createRang

Solution 1:

You have to extend the javascript code:

functionformatText(el_from, el_to, tag) {
    var selectedText = document.selection ? document.selection.createRange().text : el_from.value.substring(el_from.selectionStart, el_from.selectionEnd);// IE:Mozif (selectedText == "") {
        returnfalse;
    }

    var start_index = el_to.value.indexOf(selectedText);
    var sel_t_len = selectedText.length;
    var copy_selText = el_to.substring(start_index, sel_t_len);

    var newText='"#28'+tag+'"'+copy_selText+'"#28'+tag+'"';

    el_to.value = el_to.value.substring(0, start_index) + newText + el_to.value.substring(sel_t_len, el_to.value.length);
}

Then You can call it like this:

<button type="button" value="D" onclick="formatText(document.getElementById('message_text'), document.getElementById('message'),'D')" class="blue">D</button>

PS: I'm not sure about document.getElementById - haven't used it for years since using jQuery... You should convert to jQuery, too... You would not noeed to solve JS for IE or FF or Chrome, etc - jQuery handles this by itself...

EDIT : OK, I did a little customization and it should fit to Your needs...

Post a Comment for "Adding Tags To Selection"