Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason why most form javascript uses ID instead of NAME [duplicate]

Could someone go into the history/reasons why interacting with form elements using the NAME has gone out of practice and document.getElementById has taken over.

What exactly historically happened that prompted this change and shift.

And finally, has there been a shift or are both still recommended ways of doing things?

Document.getElementById vs  document.form.name

According to some forum discussions document.form.name is not recognized by all browsers. Is this the case? See:

"I've been told in the past that you should not use "document.form_name.element_name" compared to "document.getElementById()", as the first is not recognized by all browsers. "
  • from: http://forums.devshed.com/javascript-development-115/document-getelementbyid-vs-document-form-name-element-432059.html
like image 734
Menelaos Avatar asked Oct 05 '22 10:10

Menelaos


1 Answers

The property NAME is not necessarily unique. For example, radio buttons are grouped by having the same name. Calling getElementByName would return all buttons in the set. ID is meant to be unique. So, to answer your question, each has their place.

like image 53
x0n Avatar answered Oct 10 '22 03:10

x0n