Here is my solution. The code should be simple enough to follow, but here is the idea:
- get all inputs, select and text fields
- filter all buttons and hidden fields
- filter for allowed, visible fields only
- select first
- focus selected field
The code:
function focusFirst(parent) { $(parent).find('input, textarea, select') .not('input[type=hidden],input[type=button],input[type=submit],input[type=reset],input[type=image],button') .filter(':enabled:visible:first') .focus(); }
Then just call focusFirst with the parent or selector.
Selection:
focusFirst('#parentId');
Element
var el = $('#parentId'); focusFirst(el);
source share