- 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.
- a variable in an object, it is called a property.
- a function in an object, it is called a method.
- Dot notation is used to access object properties and methods
- bracket notation is used to access object properties
- 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.
DOM Queries are the methods that find elements in DOM tree.
-
-
- This method returns all the elements that matches the specified Tag name.
- document.getElementsByTagName('h1') returns a collection of items matching the tag name h1.
- document.getElementsByTagName('li') returns a list of 5 elements as we have five li tags in our page.
- And any individual element can be selected by using it’s index such as document.getElementsByTagName('li')[ 0 ]
-
- This selector is very similar to getElementsByTagName except the selection is based on Class name and not Tag name. You can see some examples from the image above.
-
- this selector returns only the first matched element while ignoring the remaining.
-
- This one returns the first element that matches the specified CSS selector in the document.
- document.querySelector('li') returns the first element that matches the CSS selector li. Remaining elements are ignored.
- As you can see, we can use all kinds of CSS selectors within the querySelector method that we will use in a normal CSS file.
-
- This method returns all the elements that match the specified CSS selector in the document.
- document.querySelectorAll('.heading') returns a list of all elements that matches the specified CSS selector. Since we have only one element under the class name .heading, the list contains one element. And it can be accessed by it’s index.
-
-
NodeList is DOM query that returns more than one element.
- getElementsByTagName()
- getElementsByClassName()
- querySelectorAll()
-
-
The item() method returns a node at the specified index in a NodeList object.
-
The nodes are sorted as they appear in the source code, and the index starts at 0.
-
A Node object's collection of child nodes is an example of a NodeList object.
var div = document.getElementById("myDIV"); var nodelist = div.getElementsByClassName("child"); var i; for (i = 0; i < nodelist.length; i++) { nodelist[i].style.backgroundColor = "red";}
- The className property sets or returns the class name of an element (the value of an element's class attribute).
- Set the class for a < div > element with id="myDIV": document.getElementById("myDIV").className = "mystyle";
- When you have an element node, you can select another element in relation to it using these five properties. This is known as traversing the DOM.
-
node.textContent
-
- The innerText property sets or returns the text content of the specified node, and all its descendants. If you set the innerText property, any child nodes are removed and replaced by a single Text node containing the specified string.
- The innerHTML property sets or returns the HTML content (inner HTML) of an element.
-
offers another technique to add new content to a page (rather than innerHTML). It involves three steps:
- 1- createEl ement ()
- 2- createTextNode()
- 3- appendChild()
- This Process achives by getattribute() method
- The getAttribute() method returns the value of the attribute with the specified name, of an element.
- This Process achives by setattribute() method
- This Process achives by removeattribute() method












