Here\'s a tricky one to start the morning.
I have a series of icons. When you click an icon it loads a form. Some of the forms have input[text] others have textareas
$(":input:first").focus();
The :input selector grabs all input, textarea, select and button elements.
Here it is:
$('#form-id :input:enabled:visible:first').focus();
#form-id is ID of the form; :input selects any input and textarea element; :enabled ensures the input is editable; :viisble ensures that the element is visible; :first is obvious
EDIT
This is actually not that great of a solution. Patricia's and Onkelborg's solutions below are much more elegant.
var $firstInput = jQuery("input:first");
var $firstTextArea = jQuery("textarea:first");
if(firstInput.length == 0) {
$firstTextArea.focus();
}
else {
$firstInput.focus();
}