I don't see any problem with this approach beside the fact that you should also get the position of the cursor to replace your selection you probably should use like is the case in this post:
$("#myTextInput").bind("keydown keypress mousemove", function() {
alert("Current position: " + $(this).caret().start);
});
jQuery: Get the cursor position of text in input without browser specific code?