Detect when “cursor position inside input change” in jQuery?

后端 未结 2 1257
野趣味
野趣味 2020-12-05 19:21

I\'m using a plugin called jQuery TextRange to get the position of cursor inside a input (in my case, a textarea) and set the position too.

But now I have one thing

2条回答
  •  自闭症患者
    2020-12-05 20:00

    I needed something like this myself, so based on @RenatoPrado solution I've created a jQuery extension (it's on npm - jquery-position-event).

    To use it you can add standard event:

    var textarea = $('textarea').on('position', function(e) {
       console.log(e.position);
    });
    

    and if you want the initial value you can use:

    var textarea = $('textarea').on('position', function(e) {
       console.log(e.position);
    }).trigger('position');
    

    The event also have useful column and line properties.

提交回复
热议问题