-1

The cursor position on focus is wrong does not default to 0 position it goes to wherever I click it does anyone have any suggestions?

 $('input[type="text"][name="name"]').focus(function() {
$(this).setCursorPosition(0);
}

Would something like the above work I can't see anything in my code thats causing this.

1 Answers1

0

It's because .setCursorPosition is not a classic jQuery method. There are, however plugins for this.

new function($) {
  $.fn.setCursorPosition = function(pos) {
    if ($(this).get(0).setSelectionRange) {
      $(this).get(0).setSelectionRange(pos, pos);
    } else if ($(this).get(0).createTextRange) {
      var range = $(this).get(0).createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  }
}(jQuery);

Taken here

Community
  • 1
  • 1
genesis
  • 50,477
  • 20
  • 96
  • 125