pro386-a

by Joe Saad

HTML

<!-- script credit to this post https://stackoverflow.com/questions/8405148/how-to-replace-my-selected-text-in-jquery --> 

<textarea rows="2" cols="20" id="pPost">mardagz
</textarea> 
<input type="button" id="bbbold" value="clcikMe" />

JavaScript

function getSelectedText() {
    var len =$("#pPost").val().length;
    debugger;
    var start = $("#pPost")[0].selectionStart;
    var end = $("#pPost")[0].selectionEnd;
    var sel = $("#pPost").val().substring(start, end);
    return sel;
}


function bbrep(start, end) {
    var tmpVal = getSelectedText();
    var str = $("#pPost").val();
    var tmpStr = str.replaceAt($("#pPost")[0].selectionStart, start + tmpVal + end) + str.substring($("#pPost")[0].selectionEnd);
    $("#pPost").val(tmpStr);
}

$("#bbbold").click(function() {
    bbrep("[b]", "[/b]");
    return false;
})

String.prototype.replaceAt=function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}