I want to make my own html text editor very simple code. And When I click Bold button, in textarea, must be insert [B]boldtext[/B] and "boldtext" can be select like this photo.

I wrote tihs code,
function TextEditor(startSyntax,endSyntax,elementID)
{
var textarea = $('#' + elementID)
var start = textarea[0].selectionStart;
var end = textarea[0].selectionEnd;
var text = textarea.val();
if (start == end) {
return text.substring(0,start) + startSyntax + "text" + endSyntax + text.substring(end,text.lenght);
};
return text.substring(0,start) + startSyntax + text.substring(start, end) + endSyntax + text.substring(end,text.lenght);
}
$("#btnKaydet").on("click", function() {
$("#txtMesaj").val(TextEditor("[B]","[/B]","txtMesaj"));
});
What I wrote inserts text but it does not select "boldtext" text after adding the tags, like in photo. How can I do this with jquery? Thanks