carrera carrera - 1 year ago 75
jQuery Question

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:


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:


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


Answer Source

You are using a variable name instead of its contents.

Try this

$("input[field='" + myField + "']").focus(); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download