Mihir
Mihir

Reputation: 8734

difference between document.all and document.forms[0]

Can any one explain what is the difference between

document.all

and the document.forms[0] please?

Thank you

Upvotes: 4

Views: 8944

Answers (3)

KooiInc
KooiInc

Reputation: 122966

document.all gives you a reference to an array-like object containing all elements of the document in Internet Explorer (IE). Document.forms[0] gives you a pointer to the first form element in the document, in all browsers

The 2 are quite different then. If your form had a name attribute, say 'myform', then in IE that form could be referenced with document.all.myform

document.all is deprecated from IE version 5 and up. You can still use it though, even in IE9 it's still available. Often it's used to test if the browser is IE:

if (document.all) {
  //o no, it's IE again! We have to do it another way!
}

Referencing forms in the form of document.forms[0] is considered bad practice. More on that can be found here

NOTE: Since this answer was first written IE11 has been introduced which dropped support for document.all See Compatibility changes in IE11 for more information

Upvotes: 5

nanda kumar
nanda kumar

Reputation: 31

at first you have use document.all.myform to access your form . in the second format you can use document.forms[0] to access your form

     both one two is same in function but two is better than one
                             document.all.myform using this your searching all the way rome.

Upvotes: 0

Harry Joy
Harry Joy

Reputation: 59690

The document.all property is an array of all the HTML elements that are in the document. while Document.forms[0] is the first form in the document. You should avoid using document.all.

Internet Explorer 4 introduced the document.all DOM (document object model) to allow access to the various parts of the web page. Soon after that the standard DOM method getElementById was introduced and is therefore available in all version 5+ browsers. This means that the document.all references are only needed to support IE4.Just about no one runs IE4 any more and so support for the document.all DOM is no longer required.

To learn more on how to use document.form[0] read this.

Upvotes: 3

Related Questions