This is a simple script that I use for wrapping a selected word in a textarea field. I'm looking to:
make sure my logic is valid/see what others have done in a relative context.
request improvements to the OO approach I'm going for.
Formatter = {};
Formatter.getSelection = function(event) {
var text = document.getElementById("mass_message");
var sPos = text.selectionStart;
var ePos = text.selectionEnd;
var front = (text.value).substring(0, sPos);
var sel = (text.value).substring(sPos, ePos);
var back = (text.value).substring(ePos, text.value.length);
format = event.data.format;
var print = Formatter.setString(text, format, front, back, sel);
}
Formatter.setString = function(text, format, front, back, sel) {
text.value = front + "<" + format + ">" + sel + "</" + format + ">" + back;
}
$(document).ready(function(){
$( '#bolden' ).on('click', { format: "b" }, Formatter.getSelection);
$( '#italicize' ).on('click', { format: "i" }, Formatter.getSelection);
});
I'm appreciative of any suggestions and am happy to answer questions about the code.