I did say to modify it.
Code:
function wrapWith(fieldId, opening, closing) {
var el = document.getElementById(fieldId);
if(document.selection) {
// I'm not sure how this would be done with IE, after looking at MSDN.
// Maybe someone else knows.
} else if(el.selectionStart || el.selectionStart == 0) {
var begin = el.value.substring(0, el.selectionStart),
end = el.value.substring(el.selectionStart + 1, el.selectionEnd),
middle = el.value.substring(el.selectionEnd + 1),
scroll = el.scrollTop;
el.value = begin + opening + middle + closing + end;
el.focus();
el.selectionStart = begin.length;
el.selectionEnd = (begin + opening + middle + closing).length;
el.scrollTop = scroll;
} else {
el.value += opening + closing;
el.focus();
}
}
May need tweaking. Definitely needs IE support. MSDN is annoyingly cryptic, and Microsoft seem to have set out to build one of the most useless APIs they possibly could for this purpose...
Bookmarks