• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Creating Dynamic Content > 14.13. Getting a Reference to an HTML Element Ob...

Getting a Reference to an HTML Element Object

NN 6, IE 5

Problem

You want to derive a valid element object reference starting with the string ID or tag name of an existing element.

Solution

Use the W3C DOM method that has scope over every element in the document:

var elem = document.getElementById("elementID");

If the element doesn’t have an id attribute assigned to it, you can reach the element by tag name. The following example retrieves an array of elements with the same tag name:

var elems = document.getElementsByTagName("tagName");

Assuming you know the position of the desired element among all elements with the same tag name, use standard array syntax to obtain a reference to the single element:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint