jQuery auto-focus on particular text field after page load

Below code will help us to focus on particular text field in page. It will be used for users to start typing their queries after page is loaded. We can use this in login, search pages etc., for quick access.

jQuery(document).ready(function() {
    var txtField = jQuery("#autocomplete").get(0);
    var elemLen = txtField.value.length;
    txtField.selectionStart = elemLen;
    txtField.selectionEnd = elemLen;
    txtField.focus();
});

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: