0

Can anyone tell me the diff between the following two statements

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

var username=document.myForms.username.value;
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by urtrivedi
0

Sorry 2nd statement is

var username=document.myForm.username.value;
0

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

Edited by Airshow: n/a

0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.