Catching "cursor place change" events inside textareas with jQuery (IE6-compatible)

Ben picture Ben · Apr 29, 2011 · Viewed 7.5k times · Source

How can I catch the event of "cursor place change" inside textarea with jQuery (also should working in IE6)?

Example1:

Before :text |

After : te|

Example2:

Before :text |

After : text tex|t2

Example3:

Before :text |

After : |


Edit:

After catching the event of cursor- need also to check if cursor change his position (also have to work for IE6)

Edit2:

If you have solution that will not work in IE6 but in IE7+webkit please write it

Answer

Cos Callis picture Cos Callis · Apr 29, 2011

There are essentially three events that can cause a cursor to change position,

  1. keystrokes

  2. mouse clicks

  3. programmatic events like paste, select, focus...

    I would capture those events for whatever it is you are trying to accomplish with 'cursor place change'

code sample added:

$("#myTextInput").bind("keydown click focus", function() {
  alert("Current position: " + $(this).caret().start);
});

Thanks to @Nick Craver