Tae-Sung Shin
Tae-Sung Shin

Reputation: 20620

browser console error for custom jquery plugin

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);

Upvotes: 0

Views: 610

Answers (1)

Gelin Luo
Gelin Luo

Reputation: 14373

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

Upvotes: 2

Related Questions