SlideShare a Scribd company logo
Introduction to JS
Aleksander Fabijan
Recap from last time
1. We introduced JavaScript and its history,
2. We learned how to debug JavaScript,
3. We introduced Variables, Statements and
Expressions,
4. We introduced Data Types (String, objects)
5. We introduced functions.
3.
DOM
The HTML DOM (Document
Object Model)
When an HTML document is loaded
into a web browser, it becomes a
document object.
Every element in an HTML document
is a node.
Introduction to js (cnt.)
Accessing Html
elements/nodes
To access a certain node, we can help ourselves with
already available functions.
Access by id:
var myNode = document.querySelector("#someID");
… this stores the reference no the node with id == “someID” to
the variable “myNode”
ID vs CLASS in HTML
A typical HTML file will have two types of identification elements:
(1) ID
(a) Unique and can only appear once!!!
(b) We refer to ID tags with “#id_name.”
(2) CLASS
(a) Class (not unique and can/should be used multiple times)
(b) We refer to “class” elements with “.class”
Use a class when you want to consistently style multiple elements
throughout the page/site.
Example use of an ID
<html>
<body>
<h1 id="myHeader">Hello World!</h1>
<button onclick="displayResult()">Change text</button>
<script>
function displayResult() {
document.getElementById("myHeader").innerHTML ="ok";
}
</script>
</body>
</html>
Example use of a CLASS
<!DOCTYPE html>
<html><head><style>
div.cities {
background-color: black;
color: white;
margin: 20px 0 20px 0;
padding: 20px;
}
</style></head><body>
<div class="cities">
<h2>London</h2>
<p>London is the capital of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</div>
<div class="cities">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
</body>
</html>
Example with both
…
<body>
<p id =”myFriendsText”> My friends: </p>
<ul id =”myFriendsList”>
<li class=”blue”>Lilly </li>
<li class=”blue”>Sara </li>
</ul>
</body>
http://html5-demos.appspot.com/shadowdom-visualizer
“How do we programmatically add a
new element(=node) to a webpage?
In natural language...
(1) We create a new element of type Text and give it a value
(2) We create a new element of type <li>.
(3) We attach the text node to <li> node
(4) We attach the <li> node to the document <ul> node.
document.createTextNode(“...”); //creates a text node
document.createElement("li"); //creates an element node
parentNode.appendChild(childNode);
Adding elements/nodes
Example 2: Add new list item to #myFriends <ul>
//creating a new text node
var textNode = document.createTextNode(“Andrew”);
//creating a new list node
var liNode = document.createElement("li");
//appending the list tex to the list node
liNode.appendChild(textNode);
//appending the list item to the list itself
document.querySelector("#myFriendsList").appendChild(liNode);
Appending a new
element/node / example 2
To add new elements to the document we (1) need to create
them, and (2) use appendChild method to actually append them.
//Creating a new paragraph node
var pNode = document.createElement(“p”);
//Create a new text node
var textNode = document.createTextNode(“Just some text”);
//append the text node to the pnode
pNode.appendChild(textNode);
//appending a paragraph node to the body
document.querySelector("body").appendChild(pNode);
3.
Event listeners
Reacting on Events in JS
JavaScript needs a way of detecting
user actions so that it knows when to
react.
For that, we use event listeners.
http://www.quirksmode.org/js/introevents.html
.addEventListener method
element.addEventListener(event,
function, useCapture);
▷ The first parameter is the type of the event (like
"click" or "mousedown").
▷ The second parameter is the function we want to
call when the event occurs.
▷ The third parameter is a boolean value specifying
whether to use event bubbling or event capturing.
This parameter is optional.
Example
<!DOCTYPE html>
<html>
<body>
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click", displayDate);
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
Example from Before
function myAddFunction(randomText)
{
var pNode = document.createTextNode(randomText);
var liNode = document.createElement("li");
liNode.appendChild(pNode);
document.querySelector("#myFriendsList").appendChild(liNode);
}
document.querySelector("#addFriend").addEventListener("click", function() {
myAddFunction("random_text_here");}, false);
Summary
(1) We introduced the DOM,
(2) We repeated what #id and .class are,
(3) We learned how to create new nodes and
add them to the DOM,
(4) We learned how to handle events.
4.
Resources
Useful resources
https://jsbin.com (write and execute JS online)
https://jsfiddle.net/ (JS, CSS and HTML playground)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_r
e-introduction_to_JavaScript (a re-intro to JS)
https://www.kirupa.com/html5/event_capturing_bubbling_java
script.htm (event bubling)
Thanks!
You can find me at:
aleksander.fabijan@mah.se

More Related Content

What's hot (17)

PPTX
JQuery
Jacob Nelson
 
PPT
JavaScript: Ajax & DOM Manipulation
borkweb
 
PPTX
jQuery
i.omar
 
PDF
Configuration Entities in Drupal 8
Eugene Kulishov
 
PDF
Unit 4(it workshop)
Dr.Lokesh Gagnani
 
PPTX
Web programming
renukarenuka9
 
PPTX
Css Selectors
Igor Ognichenko
 
PDF
Javascript projects Course
Laurence Svekis ✔
 
PPTX
Jquery
Zoya Shaikh
 
PDF
[2019] Spring JPA의 사실과 오해
NHN FORWARD
 
PPTX
Document Object Model (DOM)
GOPAL BASAK
 
PPT
Xml dom & sax by bhavsingh maloth
Bhavsingh Maloth
 
PPTX
Chapter 18
application developer
 
TXT
Vb database connections
Tharsikan
 
PPTX
INTRODUCTION TO DOM AND DOM TREE
systematiclab
 
JQuery
Jacob Nelson
 
JavaScript: Ajax & DOM Manipulation
borkweb
 
jQuery
i.omar
 
Configuration Entities in Drupal 8
Eugene Kulishov
 
Unit 4(it workshop)
Dr.Lokesh Gagnani
 
Web programming
renukarenuka9
 
Css Selectors
Igor Ognichenko
 
Javascript projects Course
Laurence Svekis ✔
 
Jquery
Zoya Shaikh
 
[2019] Spring JPA의 사실과 오해
NHN FORWARD
 
Document Object Model (DOM)
GOPAL BASAK
 
Xml dom & sax by bhavsingh maloth
Bhavsingh Maloth
 
Vb database connections
Tharsikan
 
INTRODUCTION TO DOM AND DOM TREE
systematiclab
 

Viewers also liked (9)

PDF
Javascript intro for MAH
Aleksander Fabijan
 
PDF
jQuery and Ajax
Anton Tibblin
 
PDF
Bootstrap 3 Basic - Bangkok WordPress Meetup
Woratana Perth Ngarmtrakulchol
 
PDF
VT17 - DA355A - Introduktion till CSS
Anton Tibblin
 
ODP
History of JavaScript
Rajat Saxena
 
PDF
VT17 - DA355A - Kursintroduktion
Anton Tibblin
 
PDF
LocalStorage
Anton Tibblin
 
PDF
Introduction to data visualisation with D3
Aleksander Fabijan
 
PDF
ES6: The Awesome Parts
Domenic Denicola
 
Javascript intro for MAH
Aleksander Fabijan
 
jQuery and Ajax
Anton Tibblin
 
Bootstrap 3 Basic - Bangkok WordPress Meetup
Woratana Perth Ngarmtrakulchol
 
VT17 - DA355A - Introduktion till CSS
Anton Tibblin
 
History of JavaScript
Rajat Saxena
 
VT17 - DA355A - Kursintroduktion
Anton Tibblin
 
LocalStorage
Anton Tibblin
 
Introduction to data visualisation with D3
Aleksander Fabijan
 
ES6: The Awesome Parts
Domenic Denicola
 
Ad

Similar to Introduction to js (cnt.) (20)

PPTX
Web technologies-course 09.pptx
Stefan Oprea
 
PPTX
Learning About JavaScript (…and its little buddy, JQuery!)
Julie Meloni
 
PDF
Web 6 | JavaScript DOM
Mohammad Imam Hossain
 
PDF
Java script
Yoga Raja
 
PPTX
DOM and Events
Julie Iskander
 
PPTX
Webdev bootcamp
DSCMESCOE
 
PPTX
DOM (document Object Model) in java Script).pptx
powerofthehelios
 
PPTX
JavaScript APIs you’ve never heard of (and some you have)
Nicholas Zakas
 
PDF
JavaScript DOM & event
Borey Lim
 
PPT
6867389.ppt
SunilChaluvaiah
 
PPT
6867389.ppt
SunilChaluvaiah
 
PPT
6867389 (1).ppt
SunilChaluvaiah
 
PPTX
Javascript for web Programming creating and embedding with html
E.M.G.yadava womens college
 
PDF
Modern JavaScript Programming
Wildan Maulana
 
PPTX
Dom date and objects and event handling
smitha273566
 
PPTX
Dsc Charusat Learning React Part 1
JainamMehta19
 
PPT
Html dom part-ii
H K
 
PPTX
Introduction to JavaScript DOM and User Input.pptx
GevitaChinnaiah
 
PDF
Interacting with the DOM (JavaScript)
Florence Davis
 
PPT
lecture 6 javascript event and event handling.ppt
ULADATZ
 
Web technologies-course 09.pptx
Stefan Oprea
 
Learning About JavaScript (…and its little buddy, JQuery!)
Julie Meloni
 
Web 6 | JavaScript DOM
Mohammad Imam Hossain
 
Java script
Yoga Raja
 
DOM and Events
Julie Iskander
 
Webdev bootcamp
DSCMESCOE
 
DOM (document Object Model) in java Script).pptx
powerofthehelios
 
JavaScript APIs you’ve never heard of (and some you have)
Nicholas Zakas
 
JavaScript DOM & event
Borey Lim
 
6867389.ppt
SunilChaluvaiah
 
6867389.ppt
SunilChaluvaiah
 
6867389 (1).ppt
SunilChaluvaiah
 
Javascript for web Programming creating and embedding with html
E.M.G.yadava womens college
 
Modern JavaScript Programming
Wildan Maulana
 
Dom date and objects and event handling
smitha273566
 
Dsc Charusat Learning React Part 1
JainamMehta19
 
Html dom part-ii
H K
 
Introduction to JavaScript DOM and User Input.pptx
GevitaChinnaiah
 
Interacting with the DOM (JavaScript)
Florence Davis
 
lecture 6 javascript event and event handling.ppt
ULADATZ
 
Ad

More from Aleksander Fabijan (8)

PPTX
Retrospective 1
Aleksander Fabijan
 
PPTX
Python oop third class
Aleksander Fabijan
 
PPTX
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
PPTX
Python oop class 1
Aleksander Fabijan
 
PDF
Introduction to OOP in python inheritance
Aleksander Fabijan
 
PPTX
Introduction to OOP in Python
Aleksander Fabijan
 
PDF
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
PDF
JavaScript development methodology
Aleksander Fabijan
 
Retrospective 1
Aleksander Fabijan
 
Python oop third class
Aleksander Fabijan
 
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
Python oop class 1
Aleksander Fabijan
 
Introduction to OOP in python inheritance
Aleksander Fabijan
 
Introduction to OOP in Python
Aleksander Fabijan
 
The evolution of continuous experimentation in software product development: ...
Aleksander Fabijan
 
JavaScript development methodology
Aleksander Fabijan
 

Recently uploaded (20)

PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Python basic programing language for automation
DanialHabibi2
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 

Introduction to js (cnt.)

  • 2. Recap from last time 1. We introduced JavaScript and its history, 2. We learned how to debug JavaScript, 3. We introduced Variables, Statements and Expressions, 4. We introduced Data Types (String, objects) 5. We introduced functions.
  • 4. The HTML DOM (Document Object Model) When an HTML document is loaded into a web browser, it becomes a document object. Every element in an HTML document is a node.
  • 6. Accessing Html elements/nodes To access a certain node, we can help ourselves with already available functions. Access by id: var myNode = document.querySelector("#someID"); … this stores the reference no the node with id == “someID” to the variable “myNode”
  • 7. ID vs CLASS in HTML A typical HTML file will have two types of identification elements: (1) ID (a) Unique and can only appear once!!! (b) We refer to ID tags with “#id_name.” (2) CLASS (a) Class (not unique and can/should be used multiple times) (b) We refer to “class” elements with “.class” Use a class when you want to consistently style multiple elements throughout the page/site.
  • 8. Example use of an ID <html> <body> <h1 id="myHeader">Hello World!</h1> <button onclick="displayResult()">Change text</button> <script> function displayResult() { document.getElementById("myHeader").innerHTML ="ok"; } </script> </body> </html>
  • 9. Example use of a CLASS <!DOCTYPE html> <html><head><style> div.cities { background-color: black; color: white; margin: 20px 0 20px 0; padding: 20px; } </style></head><body> <div class="cities"> <h2>London</h2> <p>London is the capital of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p> </div> <div class="cities"> <h2>Paris</h2> <p>Paris is the capital and most populous city of France.</p> </div> </body> </html>
  • 10. Example with both … <body> <p id =”myFriendsText”> My friends: </p> <ul id =”myFriendsList”> <li class=”blue”>Lilly </li> <li class=”blue”>Sara </li> </ul> </body> http://html5-demos.appspot.com/shadowdom-visualizer
  • 11. “How do we programmatically add a new element(=node) to a webpage?
  • 12. In natural language... (1) We create a new element of type Text and give it a value (2) We create a new element of type <li>. (3) We attach the text node to <li> node (4) We attach the <li> node to the document <ul> node. document.createTextNode(“...”); //creates a text node document.createElement("li"); //creates an element node parentNode.appendChild(childNode);
  • 13. Adding elements/nodes Example 2: Add new list item to #myFriends <ul> //creating a new text node var textNode = document.createTextNode(“Andrew”); //creating a new list node var liNode = document.createElement("li"); //appending the list tex to the list node liNode.appendChild(textNode); //appending the list item to the list itself document.querySelector("#myFriendsList").appendChild(liNode);
  • 14. Appending a new element/node / example 2 To add new elements to the document we (1) need to create them, and (2) use appendChild method to actually append them. //Creating a new paragraph node var pNode = document.createElement(“p”); //Create a new text node var textNode = document.createTextNode(“Just some text”); //append the text node to the pnode pNode.appendChild(textNode); //appending a paragraph node to the body document.querySelector("body").appendChild(pNode);
  • 16. Reacting on Events in JS JavaScript needs a way of detecting user actions so that it knows when to react. For that, we use event listeners. http://www.quirksmode.org/js/introevents.html
  • 17. .addEventListener method element.addEventListener(event, function, useCapture); ▷ The first parameter is the type of the event (like "click" or "mousedown"). ▷ The second parameter is the function we want to call when the event occurs. ▷ The third parameter is a boolean value specifying whether to use event bubbling or event capturing. This parameter is optional.
  • 18. Example <!DOCTYPE html> <html> <body> <button id="myBtn">Try it</button> <p id="demo"></p> <script> document.getElementById("myBtn").addEventListener("click", displayDate); function displayDate() { document.getElementById("demo").innerHTML = Date(); } </script> </body> </html>
  • 19. Example from Before function myAddFunction(randomText) { var pNode = document.createTextNode(randomText); var liNode = document.createElement("li"); liNode.appendChild(pNode); document.querySelector("#myFriendsList").appendChild(liNode); } document.querySelector("#addFriend").addEventListener("click", function() { myAddFunction("random_text_here");}, false);
  • 20. Summary (1) We introduced the DOM, (2) We repeated what #id and .class are, (3) We learned how to create new nodes and add them to the DOM, (4) We learned how to handle events.
  • 22. Useful resources https://jsbin.com (write and execute JS online) https://jsfiddle.net/ (JS, CSS and HTML playground) https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_r e-introduction_to_JavaScript (a re-intro to JS) https://www.kirupa.com/html5/event_capturing_bubbling_java script.htm (event bubling)