barrymac
barrymac

Reputation: 2760

how does one extend or change the jquery focus function in a chain?

Given the following:

   $("#dataField").focus().autocomplete({
        minLength:5,
        cache:false,
        source:"${request.contextPath}/ajaxActions/getdata",
        select:function (event, ui) {
            populateForm(ui);
        }
    });

How could I change the focus function such that it would place the cursor at the end of the input's current value?

Upvotes: 1

Views: 888

Answers (2)

KOGI
KOGI

Reputation: 3989

I haven't actually tested this, but it should more or less do what you need.

(function( )
{
    var oldFocus = $.fn.focus;
    $.fn.focus = function( arg1, arg2 )
    {
        // ======== Call Old Function
        var result = oldFocus.apply( this, arguments );

        // ======== Additional Functionality
        // Logic here to move your cursor

        return result; // -- this preserves ability to chain
    }
})( );

For actually moving the cursor, see this SO question: move cursor to the beginning of the input field?

Upvotes: 1

Sunjay Varma
Sunjay Varma

Reputation: 5115

 

$("#dataField").focus(function() {
      $(this).val($(this.val());
 }).focus();

This approach is the jQuery way of doing the stuff in this link and thus should do what you require.

Upvotes: 1

Related Questions