English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I read a Javascript code and found a function document.getElementByID which is supposed to return the name of an element by typing its ID.
I also have some more confusions.
How does this function help the programmer and where do you type the ID to find out the element's name?

2007-04-20 02:25:44 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

getElementById is a method of the document object in the Document Object Model. It returns a reference to an object that represents an element on the page, in the case of an HTML document, or a node, in the case of an XML document. The element can then be examined or manipulated through this object. For example:





If there is no element with the given ID, it returns null. The behavior of getElementById is not defined if more than one element has this ID, although in practice some web browsers return the first element with the given ID.

2007-04-20 02:38:40 · answer #1 · answered by Prashant Sharma 2 · 1 0

The name attribute is "deprecated" in favor of the id attribute. However, there are times in which the name attribute *must* be used, particularly in form elements. It does no harm to specify both, and even with the same value. The id attribute is specified in an html element in the same manner as a name attribute would be. For example, in a paragraph, you'd specify,

Some words go here.



Then a getElementById would return a reference to that paragraph.

Hope this helps.

2007-04-20 13:22:10 · answer #2 · answered by fjpoblam 7 · 0 0

It's useful to the programmer because he/she can process all elements of a HTML page in one for-loop (or similar loop), instead of having to process each element individually by name. It is also useful on a dynamic page where the number of elements and their names are unknown at design time.

I don't know the commands off by heart but I'm sure if you search the internet for "JavaScript Reference" then you'll find a page that knows already.

2007-04-20 09:37:53 · answer #3 · answered by Anonymous · 0 0

In the old days an element was given a name, and it still is, but to improve the linkage between Javascript and DHTML they made the id property which is better to work with. You apply it like so:

 



You can also add the name too if you really want.

2007-04-20 09:37:16 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers