Setting focus back to the current field on field change

rss

Posted by Stu - May 10 ’16 at 07:52

I am trying to set focus back to the field that I am currently changing when click tab and the field fails validation.

When the form loads I set focus to $('.ScanBarcode-field').focus(); which works. When the user enters a value and hits "tab"

fd.field('ScanBarcode').change(function(){

//Validation rules fail

$('.ScanBarcode-field input').focus();

});

I am expecting the focus to return to the field that just changed. What am I missing?

Thanks

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 7 months 5 days
    #1 by rostislav May 11 ’16 at 05:40

    It could be a number of reasons, but my initial question is why are you doing

    $('.ScanBarcode-field input').focus();

    when

    $('.ScanBarcode-field').focus();

    is as you say what works?

  • Stu
    Member
    Member for: 3 years 7 months 2 days
    #2 by Stu May 11 ’16 at 06:03

    [#1]: Hi Rostislav

    No reason, I have tried so many variations i must have added "input" to see if it worked. I changed back to "$('.ScanBarcode-field').focus();" and still not working.

    Thanks

  • Stu
    Member
    Member for: 3 years 7 months 2 days
    #3 by Stu May 12 ’16 at 04:58

    [#2]: Are there any other sugestions?

    Thanks

  • rostislav
    rostislav
    Moderator
    Member for: 4 years 7 months 5 days
    #4 by rostislav May 12 ’16 at 06:05

    [#3]:

    I assume ScanBarcode is a text field. Try this:

    fd.field('ScanBarcode').change(function(){
    	setTimeout(function(){
    		$('.ScanBarcode-field input').focus();
    	},0);
    });
    
  • Stu
    Member
    Member for: 3 years 7 months 2 days
    #5 by Stu May 12 ’16 at 05:57

    [#4]: Thanks that worked.

Displaying 1 to 5 of 5 messages