Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

Struts form and Javascript

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?

Upvotes: 2

Views: 6543

Answers (3)

Buhake Sindi
Buhake Sindi

Reputation: 89169

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;

Upvotes: 1

Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

We cannot assign id to the form like

/so the best way to do this is to access a particular form in a sequence it appears. Same is the sequence case for form elements.

document.forms[0].elements[0]

Upvotes: 1

aroth
aroth

Reputation: 54806

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();

Upvotes: 1

Related Questions