I have a login form in struts and I want to have focus on the username field when the page loads.
I am using <html:form> tag and I cannot get this form using its name from javascript as document.formName.username .
How can I do this?
I have a login form in struts and I want to have focus on the username field when the page loads.
I am using <html:form> tag and I cannot get this form using its name from javascript as document.formName.username .
How can I do this?
Assign an id to the input element that you want to focus on, then do:
document.getElementById("myInputId").focus();
Alternately, don't give it an id, and do:
document.getElementsByName("username")[0].focus();
struts-config.xml (use <html:html xhtml="true"> if your <html:form> doesn't render an id attribute.if you have <html:form action="/someAction"> and in your struts-config.xml, you've specified a name for that action (which points to an ActionForm) and your html declaration is <html:html xhtml="true"> and inside your form you have <html:input> (with name of "userName"), then you can do this in your javascript:
document.formName.userName.value;
else
document.forms["formName"].userName.value;
struts-config.xml (use <html:html xhtml="true"> if your <html:form> doesn't render an id attribute.