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

2

7

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

Pragmatism answered 29/4, 2011 at 13:26 Comment(0)
A
16

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

Audacity answered 29/4, 2011 at 13:33 Comment(5)
thanks, 1. how can I detect cursor position after I catch it by event? 2. its posible to insert to $('textarea').bind('all event') but its create CPU problem - its should be insert to bind only needed events exectlyPragmatism
thank you, 1. its not catch "delete by the mouse" new cursor position 2. plugin to heavy 2,299 bytesPragmatism
@Yosef: please explain #1? Mouse doesn't delete. You might want to add context menu action, delete, to the list of events you are capturing. as for #2, if you get the latest version of jquery (1.5.2) in the minified version, it is 83k. (you want to debug/dev against the full version and then switch to minified for deployment. How is 2.3 k "too heavy"?Audacity
ok, I mean in #1 that user can delete with keyboard or with the mouse chars inside textarea - and cursor change his position.if user delete by keyboard you catch it but with the mouse you not catch with "keydown click focus".Pragmatism
@Yosef, by your description "mouse delete" should be captured as either a keydown event or a keyup. (The cursor position may be changed by the delete happening between keydown and keyup).Audacity
B
2

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

Basket answered 5/7, 2018 at 18:6 Comment(1)
Doesn't include arrow keysEndothecium

© 2022 - 2024 — McMap. All rights reserved.