- WHAT IS AN OBJECT?
Objects 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
- If a function is part of an object, it is called a method.
- The value of a method is always a function.
- An object cannot have two keys with the same name.
- How to creat an obect?
- one of the metods is : Literal notation
- Accessing an object used : dot notation or sqare brackets
- you can use the object name followed by the method
name.hotel .checkAvailability()
- What is 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 is neither part of HTML, nor part of JavaScript; it is a separate set of rules.
- (the DOM tree) is made of objects.
- DOM an Application Programming Interface (API).
- As a browser loads a web page, it creates a model of that page. The model is called a DOM tree, and it is stored in the browsers' memory. It consists of four main types of nodes.
- Every element, attribute, and piece of text in the HTML is represented by its own DOM node.
- Each node is an object with methods and properties.
- Scripts access and update this DOM tree (not the source HTML file).
- Any changes made to the DOM tree are reflected in the browser.
- WORKING WITH THE DOM TREE
- Locate the node that represents the element you want to work with.
- Use its text content, child elements, and attributes.
-
Step1:ACCESS THE ELEMENTS
- select an individual element:
getElementByld ()
,querySe1ector ()
- select multiple elements:
getElementsByClassName()
,getElementsByTagName()
,querySelectorAll()
- move from one element node to a related element node:
parentNode
: Selects the parent of the current element node (which will return just one element).previousSibl ing / nextSibl ing
: Selects the previous or next sibling from the DOM tree.firstChild / lastChild
:Select the first or last child of the current element.
- select an individual element:
-
The terms elements and element nodes are used interchangeably but when people say the DOM is working with an element, it is actually working with a node that represents that element.
-
Step2: WORK WITH THOSE ELEMENTS
- ACCESS OR UPDATE ATTRIBUTE VALUES:
- Select the
<li>
element - Use the
firstChild
property to get the text node - Use the text node's only property (nodeValue) to get the text from the element
nodeValue
This property lets you access or update contents of a text node.
- Select the
- WORK WITH HTML CONTENT
- innerHTML : access to child elements and text content
- extContent: just the text content
- create new nodes, add nodes to a tree, and remove nodes from a tree: create Element(),createTextNode(), appendChild () / removeChild (); This is called DOM manipulation.
- ACCESS OR UPDATE ATTRIBUTE VALUES
- className /id :Lets you get or update the value of the class and id attributes.
- hasAttribute(): checks if an attribute exists.
- getAttribute():gets its value.
- setAttribute():updates the value.
- removeAttribute():removes an attribute.
- ACCESS OR UPDATE ATTRIBUTE VALUES:
-
DOM queries :To find element in DOM tree.
-
caching the selection : store the location of the element in a variable
-
Nodelists :they are a type of object called a collection.
-
live Nodelist: is updated at the same time, faster to generate
-
static Nodelist : not updated to reflect the changes made by the script.
-
four different DOM queries that all return a Nodelist.
- getElementsByTagName('hl') :returns one element
- getElementsByTagName('li'): returns four elements
- getElementsByClassName('hot'): contains only three of the
<li>
elements - querySelectorA11('li[id]'): returns four elements.
-
to select an element from a Nodelist:
- THE
item()
METHOD: return an individual node from the Node list. - Array syntax
- THE