The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20130925

html javascript - set focus to first page input text textfield when page loads

here's how you set the focus in the first input text textfield of an html page when the page loads.

put this in your page template header (inside the <head> tag, or just inside an individual page's <head> tag):
<script type="text/javascript" src="/scripts/keyboard-controls.js"></script>

create a file here:
scripts/keyboard-controls.js

scripts/keyboard-controls.js

function setFocusOnFirstTextfield() {
    var nodeList = document.getElementsByTagName("input");
    for(item in nodeList) {
        if(nodeList[item].getAttribute("type") == "text" || nodeList[item].getAttribute("type") == "password") {
            nodeList[item].focus();
            break;
        }
    };
}


put this in your page template footer (or just at the bottom of an individual html page):

<script type="text/javascript">
    setFocusOnFirstTextfield();
</script>


(right before the closing body tag, </body>)

so, if you're using a template for your header and footer, then this code will work for all pages in your project :)

No comments:

Post a Comment