0

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

    })
Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63

3 Answers3

2

you could always use CSS direction: rtl; this line of code simply changes the direction of the text to Right To Left and then u could just add the css class to your html elements using .addClass("your_class")

It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.

Edit: Try putting .addClass("your_class") outside the keyup function (although that might still not give you the best result you are looking for), check my comment below for a better way to do it.

Update: here is another way to do it

$('input[type="text"]').change(function () {
$(this).val() = $(this).split("").reverse().join("");
});
Rimon
  • 178
  • 1
  • 10
  • if you add the class to the html element from the beginning and not by dom manipulation using jquery it should work. because when you use jquery the changes are applied after the page has been fully rendered by the browser. – Rimon Jul 19 '12 at 11:18
2

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.

Community
  • 1
  • 1
techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

try this, it will keep the cursor blinking at the beginning of the text in the input box

$('input[type="text"]')
    .attr('maxlength','7')
    .attr('dir', "rtl")
    .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

    })

try the demo link http://jsfiddle.net/hcWCQ/1/

in the eg: click on the input box and then press Shift+TAB and then start typing not exactly what u r looking for.... but somewhere close

swapnilsarwe
  • 1,290
  • 1
  • 8
  • 13
  • for more clearance... what u want is if you are typing `'stackoverflow'` do you want it to be written as `"wolfrevokcats"` with cursor before `w` – swapnilsarwe Jul 19 '12 at 11:14