Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 233
Ben Avatar asked Apr 29 '11 13:04

Ben


2 Answers

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

like image 69
Cos Callis Avatar answered Nov 03 '22 19:11

Cos Callis


I've found that the select event seems to cover all changes to caret position.

like image 1
Ryder Bergerud Avatar answered Nov 03 '22 18:11

Ryder Bergerud