Posted By

kouphax on 11/10/08


Tagged

javascript focus


Versions (?)

Auto Focus First On-Screen Element


 / Published in: JavaScript
 

This code, when placed at the end of the body tag will set focus on the first visible enabled element on the screen. It considers visibility as well as all forms on-screen

  1. (function(){
  2. var forms = document.forms || [];
  3. for(var i = 0; i < forms.length; i++){
  4. for(var j = 0; j < forms[i].length; j++){
  5. if(forms[i][j].type != "hidden" && forms[i][j].disabled != true && forms[i][j].style.display != 'none'){
  6. forms[i][j].focus();
  7. return;
  8. }
  9. }
  10. }
  11. })();

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jrevillini on April 30, 2009

one more condition should be added to make this snip compatible with forms which contain fieldsets:

&& forms[i][j].nodeName.toLowerCase() != 'fieldset'

or something like that.

Posted By: kouphax on July 28, 2009

Point taken. I'll look into this soon.

James.

You need to login to post a comment.