Skip to content

Instantly share code, notes, and snippets.

@powdahound
Created July 23, 2009 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save powdahound/153203 to your computer and use it in GitHub Desktop.
Save powdahound/153203 to your computer and use it in GitHub Desktop.
function focusFirstInput() {
var inputElements = document.getElementsByTagName('input');
try {
for (var i = 0; i < inputElements.length; i++) {
var e = inputElements[i];
// uses jQuery for hasClass()
if (e.tagName == 'INPUT' &&
!$(e).hasClass('nofocus') &&
(e.type == "text" || e.type == "password") &&
e.value === '') {
e.focus();
return;
}
}
} catch (e) {} // in case IE gets angry
}
window.onload = focusFirstInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment