1. #1
    Sencha User Saeven's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Canada
    Posts
    419
    Vote Rating
    1
    Saeven is on a distinguished road

      0  

    Question 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

  2. #2
    Ext JS Premium Member
    Join Date
    Mar 2007
    Location
    Virginia, USA
    Posts
    220
    Vote Rating
    0
    dfenwick will become famous soon enough

      0  

  3. #3
    Sencha User Saeven's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Canada
    Posts
    419
    Vote Rating
    1
    Saeven is on a distinguished road

      0  

    Default


    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.

  4. #4
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    So you're just doing string manipulation on the value then? Nothing special needed, just manipulate the string!

  5. #5
    Sencha User Saeven's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Canada
    Posts
    419
    Vote Rating
    1
    Saeven is on a distinguished road

      0  

    Default


    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); 
        } 
    }

Thread Participants: 2