0

I found this simple jquery plugin from jQuery Set Cursor Position in Text Area and it's working great. But one issue is I am getting an error in firefox and chrome consoles

Uncaught TypeError: Object #<HTMLInputElement> has no method 'setCursorPosition'

What I am doing is really simple as following.

<script type="text/javascript">
//to set text cursor
(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);
....
textboxArray.get(0).setCursorPosition(0);
</script>

Any hint for the reason I am getting the error above will be appreciated.

Update: Thanks to Blender, embarrassingly following solved the problem.

textboxArray.setCursorPosition(0);
Community
  • 1
  • 1
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240

1 Answers1

2

It looks that your textboxArray elements are not jQuery object, try $(textboxArray.get(0)).setCurrsorPosition(0);

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • I think you have right direction. But textboxArray is jquery array in fact. Thing is the plugin expects array rather than element as this. Thanks anyway. – Tae-Sung Shin Jan 21 '12 at 00:53
  • jQuery array doesn't mean all element in the array is a jQuery object. How did you create that array? – Gelin Luo Jan 21 '12 at 03:26
  • It's all jquery object because they are result from jquery selector. Anyway, I resolved the problem and happy again. Thanks for your answer. – Tae-Sung Shin Jan 21 '12 at 04:35