Playing with DOM elements
There are a lot of things we can do with the page we are accessing beyond getting the title of the document, and this can be done with a little help from the Document Object Model API. We are not going to discuss each object and function of the DOM API, but we will touch on some that are very useful. If you want to learn more about DOM API, the best place to start is the Mozilla Development Network: https://developer.mozilla.org/en-US/docs/DOM.
Selecting elements
Everything starts with the document
object, and it contains nested elements. To select an element, we either traverse the entire document or use the DOM selectors. There are different methods to reference a document element, which can be done by element ID, class, name, tag, or XPath.
|
This retrieves the element using a unique ID |
|
This selects the element using the element class name |
|
This provides a reference using the element name |
|