Objects, and the DOM
Chapter 3 “Object Literals”
Object & Method
- Object group together a set of variables and functions to create a model of a something you would recognize from the real world. In an object, variables and functions take on new names.
- If a variable is part of an object, it is called a property. Properties te ll us about the object, such as the name of a hotel or the number of rooms it has. Each individual hotel might have a different name and a different number of rooms.
- If a function is part of an object, it is called a method.Methods represent tasks that are associated with the object. For example, you can check how many rooms are available by subtracting the number of booked rooms from the total number of rooms.
- properties and methods have a name and a value.
- In an object, that name is called a key, each key should be a uniqe and cannot have 2 keys with the same name.
Example with some lines of code

Chapter 5: “Document Object Model (DOM)”

-
The Document Object Model (DOM) specifies how browsers should create a model of an HTML page and how JavaScript can access and update the contents of a web page while it is in the browser window.
- The DOM specifies the way in which the browser should structure this model using a DOM tree.
- The DOM is called an object model because the model (the DOM tree) is made of objects.
- Each object represents a different part of the page loaded in the browser window.
- Application Programming Interface (API). User interfaces let humans interact with programs; APls let programs (and scripts) talk to each other.
DOM Contains 4 Types of Nodes :: Each Node Is an Object With Methods and Properties :
- The Document Node it represents the entire page.
- Element Nodes HTML elements describe the structure of an HTML page. (The
<h l > - <h6>elements describe what parts are headings; the<p>tags indicate where. - Attribute Nodes The opening tags of HTML elements can carry attributes and these are represented by attribute nodes in the DOM tree. Attribute nodes are not children of the element that carries them; they are part of that element
- Text Nodes Once you have accessed an element node, you can then reach the text within that element. This is stored in its own text node.
Accessing and updating the DOM tree involves two steps:
- Locate the node that represents the element you want to work with.
- Use its text content, child elements, and attributes.
Working with The DOM Tree
- STEP 1: Access The Elements
- Select an Individual Element Node :
getElementByld ('id')Uses the value of an element’s id attribute (which should be unique within the page).querySelector('css selector')Uses a CSS selector, and returns the first matching element.
- Select Multiple Elements (Nodelists):
getElementsByClassName('class')Selects all elements that have a specific value for their class attribute.getElementsByTagName('tagName')Selects all elements that have the specified tag name .querySelectorAll('css selector')Uses a CSS selector to select all matching elements.
- Traversing Between Element Nodes :
parentNodeSelects the parent of the current element node (which will return just one element).previousSibl ing / nextSibl ingSelects the previous or next sibling from the DOM tree.firstChild / lastChildSelect the first or last child of the current element.
- Select an Individual Element Node :
- STEP 2: Work With Those Elements
- Access/Update Text Nodes :
nodeValueThis property lets you access or update contents of a text node.
- Work With Html Content :
innerHTMLOne property allows access to child elements and text content:textContentjust the text content: .create Element() , createTextNode() ,appendChild () / removeChild ()Several methods let you create new nodes, add nodes to a tree, and remove nodes from a tree.
- Access or Update Attribute Values :
hasAttribute()checks if an attribute exists.getAttribute()gets its value.setAttribute()updates the value.removeAttribute()removes an attribute.
- Access/Update Text Nodes :