Auto Focus First On-Screen Element


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.