Can anyone tell me the diff between the following two statements

var username=document.forms['myForm']['username'].value;

var username=document.myForms.username.value;

Sorry 2nd statement is

var username=document.myForm.username.value;

In general, the following are equivalent in javascript:

  • object.x.y.z
  • object

Your two statements may or may not both select the same document node.

I think the first version is more likely to be successful, not because of its syntax but because it addresses the collection document.forms . document.myForm... may also work but I would personally be less inclined to rely on it, cross-browser.

Whatever you use, test test test.

Airshow

Both the statements are working properly. But i want to know how the 2nd statement is also working. How it is recognizing the myForm as form name. In the 1st statement, form is a keyword. But in the 2nd statement we are not specifying myForm as form name.

MYFORM is anyway child of document so it is allowing, I think problem occurs when there are more than one form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.