Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus on a form field with a variable

I have a relatively simple issue I'm trying to resolve and can't seem to find anything about it.

I am working on existing product, after an error modal is closed I need to focus on a given form field, which at this point I will have stored in a variable.

Currently this works:

$("input[field='number']").focus();

I've simplified the variable names. No I have a variable as below;

myField = 'number'

But my attempts to focus on this are failing, I've tried:

$("input[field=myField]").focus();
$("input[field='myField']").focus();

As I said I've had a look around but sadly cannot find what I need. Any help would be greatly appreciated.

Thanks

like image 335
carrera Avatar asked Sep 22 '16 14:09

carrera


1 Answers

You are using a variable name instead of its contents.

Try this

$("input[field='" + myField + "']").focus(); 
like image 105
gurvinder372 Avatar answered Nov 13 '22 07:11

gurvinder372