JavaScript DOM
JavaScript DOM
console.dir(window);
document.body
document.body.bgColor ='red';
What is the DOM?
● DOM is constructed by browser
● Browser makes model of each element as an object
● Stores the attributes
● Load HTML browser turns it into an object
● console.dir(document)
● Every element is in body
● Child Node and show you the children
Interface between javascript and html
● Root node where everything lives inside
● http://brackets.io/
● https://developers.google.com/web/tools/chrome-devtools/
● https://developer.mozilla.org/en-US/docs/Web/API/Document_Ob
ject_Model/Introduction
● https://en.wikipedia.org/wiki/Document_Object_Model
● console.dir(document);
● console.log(document.URL);
DOM Manipulation
● Select element - update it
● Just like CSS many way to select elements content
function addItem(event){
//console.log(event);
if(event.keyCode === 13 && ele.value.length > 1){
console.log(ele.value.length);
eleUL.style.backgroundColor = "yellow";
}
}
Mouse Events
var eleList = document.querySelectorAll('li');
for(var x=0;x<eleList.length;x++){
console.log(eleList[x]);
eleList[x].addEventListener('mouseover',function(){
this.classList.add('red');
});
eleList[x].addEventListener('mouseout',function(){
this.classList.remove('red');
})
}
Create new element
clicker.addEventListener('click',function(){
var li = document.createElement('li');
var allListItems = document.querySelectorAll('li');
var textValue = 'test '+(allListItems .length + 1)
var tempNode =
document.createTextNode(textValue);
li.appendChild(tempNode);
mainList.appendChild(li);
})
Thank you
Thank you for taking the course, and reading this PDF. If you have any questions of suggestions
please connect with me on Udemy.
https://www.udemy.com/user/lars51/
Laurence Svekis