5

Possible Duplicate:
How to get caret position in textarea?

If i type * in anywhere in the html textarea control i need to get the current position on keyup event like "Welcome* to jQuery". So i have * after Welcome means at 8th position. Let me know if anybody can help me on this.

Community
  • 1
  • 1
Pradeep
  • 4,612
  • 10
  • 38
  • 50

2 Answers2

5

This will work. (note: with quotes it's at 8 else at 7)

$("#tf").on('keyup', function(){
    console.log($(this).val().indexOf('*'));
});​

http://jsfiddle.net/Vandeplas/hc6ZH/

UPDATE: solution with multiple *

$("#tf").on('keyup', function(){
    var pos = [],
        lastOc = 0,
        p = $(this).val().indexOf('*',lastOc);

    while( p !== -1){
        pos.push(p);
        lastOc = p +1;
        p = $(this).val().indexOf('*',lastOc);
    }
    console.log(pos);
});​

http://jsfiddle.net/Vandeplas/hc6ZH/1/

UPDATE: giving only the position of the * char you just typed

(function ($, undefined) {
    $.fn.getCursorPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if('selectionStart' in el) {
            pos = el.selectionStart;
        } else if('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
})(jQuery);

$("#tf").on('keypress', function(e){
    var key = String.fromCharCode(e.which);
    if(key === '*') {
        var position = $(this).getCursorPosition();
        console.log(position);
    } else {
        return false;
    }
});​

http://jsfiddle.net/Vandeplas/esDTj/1/

VDP
  • 6,340
  • 4
  • 31
  • 53
  • What if you press * twice? Then you will only get the index of the first one. – MiniGod Oct 09 '12 at 08:01
  • @MiniGod is right. I need the position of * that fires the keyup event not the others. – Pradeep Oct 09 '12 at 08:29
  • Didn't knew that was required I'll update ;) – VDP Oct 09 '12 at 08:29
  • @Pradeep http://stackoverflow.com/questions/263743/how-to-get-caret-position-in-textarea – MiniGod Oct 09 '12 at 08:34
  • Your answer only lists all occurrences of `*`? How about this http://jsfiddle.net/esDTj/ – MiniGod Oct 09 '12 at 09:05
  • @MiniGod your fiddle isn't working at all. keyup seems to return bad keycodes, so I switched to keypress. Nevertheless, I must agree I misinterpreted. I updated my answer and applied one of the solutions in the post Pradeep found. (just to set it straight) – VDP Oct 09 '12 at 09:34
  • You consolidated very well. Appreciate your effort. – Pradeep Oct 10 '12 at 11:51
1

Below link helped me to resolve this using jQuery itself.

Cursor position in a textarea (character index, not x/y coordinates)

Thanks for your efforts guys.

Community
  • 1
  • 1
Pradeep
  • 4,612
  • 10
  • 38
  • 50
  • Oeps, misinterpreted the question. Now that I see what you needed ;) good 4 you! Cheers! – VDP Oct 09 '12 at 09:01