views:

2612

answers:

7

Is there a simple way to set the focus (input cursor) of a web page on the first input element (textbox, dropdownlist, ...) on loading the page without having to know the id of the element?

I would like to implement it as a common script for all my pages/forms of my web application.

Thanks!

+3  A: 

There's a write-up here that may be of use: Set Focus to First Input on Web Page

Galwegian
+2  A: 
document.forms[0].elements[0].focus();

This can be refined using a loop to eg. not focus certain types of field, disabled fields and so on. Better may be to add a class="autofocus" to the field you actually do want focused, and loop over forms[i].elements[j] looking for that className.

Anyhow: it's not normally a good idea to do this on every page. When you focus an input the user loses the ability to eg. scroll the page from the keyboard. If unexpected, this can be annoying, so only auto-focus when you're pretty sure that using the form field is going to be what the user wants to do. ie. if you're Google.

bobince
+1  A: 

You also need to skip any hidden inputs.

for (var i = 0; document.forms[0].elements[i].type == 'hidden'; i++);
document.forms[0].elements[i].focus();
Marko Dumic
I might be wrong, but this loop doesn't have defined behaviour if forms[0] has no hidden inputs.
xtofl
Exactly. It is used to skip any leading hidden inputs. And it is written under assumption that there are non-hidden fields in the form.I could have done it with jQuery as well (I'll post another answer) but you didn't mention you want jQuery involved.
Marko Dumic
+4  A: 

If you're using the Prototype JavaScript framework then you can use the focusFirstElement method:

Form.focusFirstElement(document.forms[0]);
John Topley
+2  A: 

Putting this code at the end of your body tag will focus the first visible, non-hidden enabled element on the screen automatically. It will handle most cases I can come up with on short notice.

<script>
    (function(){
        var forms = document.forms || [];
        for(var i = 0; i < forms.length; i++){
            for(var j = 0; j < forms[i].length; j++){
                if(!forms[i][j].readonly != undefined && forms[i][j].type != "hidden" && forms[i][j].disabled != true && forms[i][j].style.display != 'none'){
                    forms[i][j].focus();
                    return;
                }
            }
        }
    })();
</script>
kouPhax
One thing to consider here is it may match elements that are not themselves hidden but an ancestor is. That would require tracking back up the DOM and testing each ancestors visibility which I feel is serious overkill for this kind of situation.
kouPhax
@kouPhax: What about <input type="text" readonly="readonly"> I think you should add such a case to your code. Normally people don't want focus on a readonly input text field. Anyway nice code and +1 thanks!
Marco Demajo
Untested but I updated it with !forms[i][j].readonly != undefined
kouPhax
+2  A: 

You can also try jQuery based method:

$(document).ready(function() {
    $('form:first *:input[type!=hidden]:first').focus();
});
Marko Dumic
It Works! I'm starting to integrate jQuery in my web application. So, I think I will stick with this approach! Many thanks, Marko!
splattne
What happens if the first form on the page is hidden in this case? Or if the first element on the form is hidden via CSS? Surely this would fail. I am being pedantic of course but that's how I roll.
kouPhax
In my case (using ASP.NET) I have just ONE form which is never hidden. So this works for me.
splattne
A: 

The most comprehensive jQuery expression I found working is (through the help of over here)

$(document).ready(function() {
    $('input:visible:enabled:first').focus();
});
ngeek