Search code examples
javascripthtmlwindowgetselection

I want my input to be written leftwards


I know this is possible in javascript by using window.getSelection() method but I'm not familiar with this selection object. Can you help me to write a code to make user's writing cursor to be always at the begining of the input string?

$('input[type="text"]')
    .attr('maxlength','7')
    .keyup(function(){
        var sel = window.getSelection();
        console.log(sel);
        // need a code for here to make the cursor (caret) at the begining of the string

    })

Solution

  • Can be done using a combo of:

    • dir = "rtl"
    • moving text box caret to 0 after every keypress

    Demo: http://jsfiddle.net/FF9Tf/1/

    Note: Uses hints/code from these answers.