JavaScript Notes 5
JavaScript Notes 5
JavaScript
HTML DOM HTML
<html>
<head>
<title>sitename<title> HEAD BODY
<body> attribute
</body> bold
</html>
Hello
document = {
title : “sitename", document
location :"http://localhost" ,
getElementById : function(),
getElementsByClassName : function(),
getElementsByTagName : function(),
… … 100 more
}
Select Elements HTML
Hello </p>
Hello
</div>
</body>
</html> document
HTMLElement JS Object
Select Elements HTML
Hello </p>
Hello
</div>
</body>
</html> document
HTMLElement
Select Elements HTML
Hello </p>
Hello
</div>
</body>
</html> document
NodeList [ p ] [ HTMLelememt ]
Select Elements HTML
Hello </p>
Hello
</div>
</body>
</html> document
HTMLElement
Select Elements HTML
HTMLCollection [ p ] [ HTMLelememt ]
Select Elements HTML
Hello </p>
Hello
</div>
</body>
</html> document
HTMLCollection [ p ] [ HTMLelememt ]
HTMLElement HTML
(reading)
HEAD BODY
const el = document .querySelector ( “#myDiv” )
TITLE
el.innerText “” DIV ID
myDiv
(writing)
HEAD BODY
const el = document .querySelector ( “#myDiv” )
TITLE DIV ID
myDiv
el.innerHTML = <p class=“bold”> Hey </p>
sitename P CLASS
Hello
<html>
const el = document .querySelector ( “.bold” ) <head>
<title>sitename<title>
</head>
e.className = “bold”
<body>
<div id=“myDiv”>
e.innerText = “Hello” <p class=“bold”>
Hey </p>
</div>
</body>
</html>
Attributes HTML
HEAD BODY
const el = document .querySelector ( “.bold” )
TITLE DIV ID
myDiv
el.getAttribute(“class”) “bold”
sitename P CLASS
bold
el.setAttribute(“class”, “bold dark”)
Hello
<html>
<head>
<title>sitename<title>
</head>
<body>
<div id=“myDiv”>
<p class=“bold”>
Hello </p>
</div>
</body>
</html>
CSS Styles HTML
HEAD BODY
const el = document .querySelector ( “.bold” )
TITLE DIV ID
el.style.color “black” myDiv
sitename P CLASS
Hello
el.style.backgroundColor = “yellow” <html>
<head>
<title>sitename<title>
el.style.border = “1px solid red” </head>
<body>
<div id=“myDiv”>
<p class=“bold”>
Hello </p>
</div>
</body>
</html>
ClassList HTML
HEAD BODY
const el = document .querySelector ( “.bold” )
TITLE DIV ID
el.classList.add(“dark”) myDiv
sitename P CLASS
bold
el.classList.remove(“dark”) Hello
<html>
<head>
el.classList.replace(“bold”, “dark”) <title>sitename<title>
</head>
<body>
<div id=“myDiv”>
<p class=“bold”>
Hello </p>
</div>
</body>
</html>
Children / Parent / Sibling HTML
HEAD BODY
const el = document .querySelector ( “#myDiv” )
TITLE DIV ID
el.children P myDiv
sitename P CLASS
Hello
el.previousSibling null <html>
<head>
el.nextSibling null <title>sitename<title>
</head>
<body>
<div id=“myDiv”>
<p class=“bold”>
Hello </p>
</div>
</body>
</html>
Events HTML
HEAD BODY
const el = document .querySelector ( “.bold” )
TITLE DIV ID
myDiv
el.addEventListener( “click”, function(){
sitename P CLASS
bold
})
Hello
<html>
runs on every click <head>
<title>sitename<title>
</head>
<body>
<div id=“myDiv”>
<p class =“bold”>
`
Hello </p>
</div>
</body>
</html>
Event Object HTML
HEAD BODY
const el = document .querySelector ( “.bold” )
TITLE DIV ID
myDiv
el.addEventListener( “click”, function(e){
sitename P CLASS
bold
})
Hello
<html>
<head>
event Object <title>sitename<title>
</head>
<body>
e.target.innerText <div id=“myDiv”>
<p class=“bold”>
Hello </p>
</div>
</body>
target = element </html>
Event Bubbling HTML
HEAD
const body = document .querySelector ( “body” ) BODY
TITLE DIV ID
myDiv
sitename P CLASS
bold
Hello
<html>
<head>
<title>sitename<title>
</head>
“click” started here, <body>
<div id=“myDiv”>
and it bubbles “up” <p class=“bold”>
P => Div => Body Hello </p>
</div>
</body>
</html>
Event Delegation HTML
HEAD
const body = document .querySelector ( “body” ) BODY
TITLE DIV
body.addEventListener( “click”, function(e){ ID
myDiv
sitename P CLASS
bold
})
Hello
“body” will also <html>
capture “click” / we can delegate <head>
<title>sitename<title>
“events” to body </head>
<body>
“click” started here, <div id=“myDiv”>
<p class=“bold”>
and it bubbles “up” Hello </p>
P => Div => Body </div>
</body>
</html>
Mouse Events
mousedown event
mouseenter event
mouseleave event
mousemove event
mouseout event
mouseover event
mouseup event
click event
dblclick event
Keyboard Events
keyup event
keydown event
keypress event
Document Events
scroll event
copy event
cut event
paste event
Promise : States
Promise
initial State
PENDING
RESOLVED REJECT
DATA ERROR
Forms
<form name="myForm" action="/serverAPI" method="post">
Name: <input type="text" name=“fname">
<input type="submit" value="Submit">
</form>
})
BOM- Browser Object Model
window = {
… … 100 more
}