I'm working on "Add comment box" with some features like change [b]text[/b]
to be text with bold style and so on. The problem is when the str.replace operation in javascript done the cursor which is in the writing area come back to the start position.
$('#comment').keyup(function () {
var str = document.getElementById("comment").innerHTML;
if (str.indexOf('[b]') >= 0 && str.indexOf('[/b]') >= 0) {
var start_pos = str.indexOf('[b]') + 3;
var end_pos = str.indexOf('[/b]', start_pos);
var text_to_get = str.substring(start_pos, end_pos)
res = str.replace("[b]" + text_to_get + "[/b]", "<b>" + text_to_get + "</b>");
document.getElementById("comment").innerHTML = res;
}
});