You can't do this without some browser specific code, since they implement text select ranged slightly differently. However, there are plugins that abstract this away. For exactly what you're after, there's the jQuery Caret (jCaret) plugin.
For your code to get the position you could do something like this:
$("#myTextInput").bind("keydown keypress mousemove", function() {
alert("Current position: " + $(this).caret().start);
});
You can test it here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…