Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to put cursor in beginning of text-box onfocus [duplicate]

Possible Duplicate:
Set cursor at a length of 14 onfocus of a textbox

I am able to do that in firefox and IE. But for some reason its not working in Chrome and Safari :(

I am simply using below line onfocus

$('input:text').focus(
function(){
      document.getElementById('id').setSelectionRange(0, 0);
    });

Can someone please tell me how to do similar thing in Chrome and safari?

like image 621
Deepak Yadav Avatar asked Nov 18 '11 21:11

Deepak Yadav


People also ask

How do I move the cursor at the beginning of text field?

To move the cursor to the beginning of an input field: Use the setSelectionRange() method to move the cursor to the beginning of the input field. Call the focus() method on the input element. The focus method will move the cursor to the beginning of the element's value.

How do I add a cursor to a TextBox?

Sometimes all you have to do to make sure the cursor is inside the text box is: click on the text box and when a menu is displayed, click on "Format text box" then click on the "text box" tab and finally modify all four margins (left, right, upper and bottom) by arrowing down until "0" appear on each margin.

How do you move the cursor to the end of the text in a TextBox?

To position the cursor at the end of the contents of a TextBox control, call the Select method and specify the selection start position equal to the length of the text content, and a selection length of 0.

How do I get the cursor position in HTML?

If you ever had a specific case where you had to retrieve the position of a caret (your cursor's position) inside an HTML input field, you can do so using the selectionStart property of an input's value.


2 Answers

The problem is that Chrome (I haven't heard of Safari doing this as well, but I'll take you word for it) kills the selection after the focus event has fired, so you need to add a timer. The following is adapted from my answer here:

How to place cursor at end of text in textarea when tabbed into

However, this generally isn't good usability: it's contrary to what the user expects and removes useful functionality when using the mouse (i.e. the caret going to the location the user clicks). You can probably get around that with some handling of mousedown and mouseup events.

Live demo: http://jsfiddle.net/timdown/z9DhX/1/

Code:

function moveCaretToStart(el) {
    if (typeof el.selectionStart == "number") {
        el.selectionStart = el.selectionEnd = 0;
    } else if (typeof el.createTextRange != "undefined") {
        el.focus();
        var range = el.createTextRange();
        range.collapse(true);
        range.select();
    }
}

var textBox = document.getElementById("id");

textBox.onfocus = function() {
    moveCaretToStart(textBox);

    // Work around Chrome's little problem
    window.setTimeout(function() {
        moveCaretToStart(textBox);
    }, 1);
};
like image 118
Tim Down Avatar answered Oct 20 '22 15:10

Tim Down


Webkit is resetting the caret position as part of the focus event. You need to defer execution of your script until after the event has fully fired. Using setTimeout with a delay of 0 is good enough:

$(":text").focus(function () {
    var input = this;
    setTimeout(function() {
        input.setSelectionRange(0, 0);
    }, 0);
});

Working demo: http://jsfiddle.net/ZkqGH/1/

like image 31
gilly3 Avatar answered Oct 20 '22 16:10

gilly3