Page 1 of 1

Setting focus back to the current field on field change

Posted: 10 May 2016
by Stu
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

Re: Setting focus back to the current field on field change

Posted: 11 May 2016
by rostislav
It could be a number of reasons, but my initial question is why are you doing

Code: Select all

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

Code: Select all

$('.ScanBarcode-field').focus();
is as you say what works?

Re: Setting focus back to the current field on field change

Posted: 11 May 2016
by Stu
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

Re: Setting focus back to the current field on field change

Posted: 12 May 2016
by Stu
Are there any other sugestions?

Thanks

Re: Setting focus back to the current field on field change

Posted: 12 May 2016
by rostislav
I assume ScanBarcode is a text field. Try this:

Code: Select all

fd.field('ScanBarcode').change(function(){
	setTimeout(function(){
		$('.ScanBarcode-field input').focus();
	},0);
});

Re: Setting focus back to the current field on field change

Posted: 12 May 2016
by Stu
Thanks that worked.