[Javascript] onload focus without placing inside body tags
Massimo Foti
massimo at amila.ch
Wed Aug 3 10:31:47 CDT 2005
> I was out on the web yesterday and I came across some code that would put
> focus in the first field of my form without using the body tag and I
cannot
> find that site now. Anyone know how to do this?
// Scan the current document, looking for text fields inside forms
function tmt_placeFocus(){
var formNode = document.getElementsByTagName("form")[0];
if(formNode){
var inputNodes = formNode.getElementsByTagName("input");
tmt_focusFirst(inputNodes);
}
}
// Put focus and cursor inside the first field of a node list
function tmt_focusFirst(nodelist){
for(var i=0; i<nodelist.length; i++){
var fieldType = nodelist[i].getAttribute("type").toLowerCase();
if((fieldType == "text") || (fieldType == "password")){
nodelist[i].focus();
nodelist[i].select();
break;
}
}
}
window.onload = tmt_placeFocus;
The code above will place focus on the first text/password field contained
inside the first form of the current document.
Hope it could help
----------------------------
Massimo Foti
Tools for ColdFusion and Dreamweaver developers:
http://www.massimocorner.com
----------------------------
More information about the Javascript
mailing list