Selecting elements using DOM

Document Object Model methods used for selecting elements on a web page

Elements are represented by objects which contains useful properties and methods for working with them.

getElementById

– get element by their id

Fastest and easiest way of selecting something from the page is by select using
the element associated with the element.

Notice we have id “mainHeading” in the h1 element.

We can use

result:

getElementsByClass

– Get collection of elements by class name.

Given,

We use

results:

[h1#mainHeading.heading, h2.heading.subHeading, mainHeading: h1#mainHeading.heading]

getElementsByTagName

html:

result:

[a, a]

getElementsByName

html:

js:

result:
[a]

Leave a Reply