Possible Duplicate:
Set cursor at a length of 14 onfocus of a textbox
I have a code which lays the focus selecting certain length of substring in the text box. I want to place the cursor instead of selecting the entire string. the code is as below
<script type="text/javascript">
window.onload = function() {
$("#message").keyup(function(e){
var message = document.getElementById('message');
// Select a portion of text
createSelection(message, 0, 5);
// get the selected portion of text
var selectedText = message.value.substring(message.selectionStart, message.selectionEnd);
alert(selectedText);
});
function createSelection(field, start, end) {
if( field.createTextRange ) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end-start);
selRange.select();
} else if( field.setSelectionRange ) {
field.setSelectionRange(start, end);
} else if( field.selectionStart ) {
field.selectionStart = start;
field.selectionEnd = end;
}
field.focus();
}}
</script>
HTML part of the code
<input type="text" name="message" id="message" value="" />