-
3 Apr 2007 9:47 AM #1
General JS question: Insert text at caret position?
General JS question: Insert text at caret position?
Hi Everyone,
Perhaps unrelated to Ext, I was hopeful that someone had a snippet lying around that they wouldn't mind sharing. Javascript, is not my forte!
I have a textarea caret position (as integer, say 15). And would like to insert text at that position.
Are there cross-browser solutions at doing so?
Thanks always.
Alex
-
3 Apr 2007 12:19 PM #2
-
9 Apr 2007 10:12 AM #3
Thanks for the hint, that snippet seems to require that the text area in which the text is being inserted has focus to work. What I need, is an ability to insert text at position X for example (which may not be where the caret is positioned in the given text area).
ex:
<textarea id='test'>abc<textarea>
with something like
insertAt( $('test'), 1, '111' );
adjusts the text to
a111bc
in the text area.
-
9 Apr 2007 10:20 AM #4
So you're just doing string manipulation on the value then? Nothing special needed, just manipulate the string!
-
9 Apr 2007 10:24 AM #5
NM got it! Thanks
Code:function moveCursor( obj, pos ){ if( obj.createTextRange ){ var range = obj.createTextRange(); range.move("character", pos); range.select(); } else if( obj.selectionStart ){ obj.focus(); obj.setSelectionRange(pos, pos); } }


Reply With Quote