SlideShare a Scribd company logo
1 of 24
Click to add Text 
Javascript 
Presented by Bunlong VAN 
http://geekhmer.github.io 
DOM & Event
The Disclaimer
DOM 
•DOM (Document Object Modeling) 
•Retrieving Nodes 
• document.getElementById(id) 
• document.getElementsByName(name) 
• Node.getElementsByTagName(tagName)
Document Tree Structure 
<html> 
<body> 
<h1>Heading 1</h1> 
<p>Paragraph.</p> 
<h2>Heading 2</h2> 
<p>Paragraph.</p> 
</body> 
</html> 
#text 
H1 
P 
H2 
BODY 
#document 
HTML 
HEAD 
#text 
P 
#text 
#text
child, sibling, parent 
BODY 
H1 P H2 
#text 
#text 
P 
#text #text 
lastChild 
lastChild 
lastChild 
lastChild 
lastChild 
firstChild 
firstChild 
firstChild 
firstChild 
firstChild
child, sibling, parent (Con.) 
BODY 
H1 P H2 
#text 
#text 
P 
#text #text 
lastChild 
lastChild 
lastChild 
lastChild 
lastChild 
firstChild 
firstChild 
firstChild 
firstChild 
firstChild 
nextSibling nextSibling nextSibling 
previousSibling previousSibling previousSibling
child, sibling, parent (Con.) 
H1 
#text 
lastChild 
P H2 P 
#text #text #text 
lastChild 
lastChild 
lastChild 
lastChild 
firstChild 
firstChild 
firstChild 
firstChild 
firstChild 
nextSibling nextSibling nextSibling 
previousSibling previousSibling previousSibling 
parentNode parentNode 
parentNode 
parentNode 
parentNode 
BODY
child, sibling, parent (Con.) 
BODY 
H1 P H2 
#text 
#text 
P 
#text #text 
firstChild 
firstChild 
firstChild 
firstChild 
firstChild 
nextSibling nextSibling nextSibling
Child Nodes 
BODY 
0 
1 2 
P 
childNodes 
H1 H2 
P 
3
Style 
•node.style.stylename 
•background-color 
•border-radius 
•font-size 
•list-style-type 
•word-spacing 
•z-index 
•backgroundColor 
•borderRadius 
•fontSize 
•listStyleType 
•wordSpacing 
•zIndex
Making Elements 
document.createElement(tagName); 
document.createTextNode(text); 
node.cloneNode(); // clone an individual element 
node.cloneNode(true); // clone an element and all of its des
Linking Ements 
node.appendChild(new); 
node.insertBefore(new, sibling); 
node.replaceChild(new, old); 
old.parentNode.replaceChild(new, old);
Removing Elements 
node.removeChild(old); // It returns the node 
old.parentNode.removeChild(old);
innerHTML 
•The W3C standard does not provide access to the HTML 
parser 
•All browsers implement Microsoft's innerHTML property 
•node.innerHTML = “<p>this is text</p>”;
Which Way is Better? 
•It is better to build or clone elements and append them to the 
document? 
•Or is it better to complile an HTML text and use innerHTML to 
realize it? 
•Favor clean code and easy maintenance 
•Favor performance only in extreme cases
Event 
•The browser has an event-driven, single-threaded, 
asynchronous programming model 
•Events are targeted to particular nodes 
•Event cause the invocation of event handler functions
Mouse Event 
The target is the topmost (z-index) node containing the 
cursor: 
•click 
•dblclick 
•mousedown 
•mousemove 
•mouseout 
•mouseover 
•mouseup
Input Event 
The target is the node having focus: 
•blur 
•change 
•focus 
•keydown 
•keyup 
•reset 
•submit
Event Handler 
•Classic 
– node[“on” + type] = handler; 
•Microsoft 
– node.attachEvent(“on” + type, handler); 
•W3C 
– node.addEventListener(type, handler, false); 
var handler = function(e) { 
e = e || event; 
var target = e.target || e.srcElement; 
… 
}
Bubbling 
Bubbling means that the event is given to the target, and then 
its parent, and so on until the event is canceled.
Why Bubble? 
•Suppose you have 100 draggable objects 
•You could attach 100 sets of event handlers to those 
objects 
•Or you could attach one set of event handlers to the 
container of the 100 objects
Cancel Bubbling 
•Cancel bubbling to keep the parent nodes from seeing th 
event: 
e.cancelBubble = true; 
If(e.stopPropagation) { 
e.stopPropagation(); 
}
Prevent Default Action 
•An event handler can prevent a browser action associated with 
the event (such as submitting a form): 
e.returnValue = false; 
If(e.preventDefault) { 
e.preventDefault(); 
} 
Return false;
Javascript dom event

More Related Content

What's hot (20)

HTML Forms
HTML FormsHTML Forms
HTML Forms
 
HTML5 audio & video
HTML5 audio & videoHTML5 audio & video
HTML5 audio & video
 
HTML5 Form Validation
HTML5 Form ValidationHTML5 Form Validation
HTML5 Form Validation
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
Java script
Java scriptJava script
Java script
 
Dom
DomDom
Dom
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Flexbox
FlexboxFlexbox
Flexbox
 
HTML5 & CSS3
HTML5 & CSS3 HTML5 & CSS3
HTML5 & CSS3
 
Css3
Css3Css3
Css3
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
Html 5 New Features
Html 5 New FeaturesHtml 5 New Features
Html 5 New Features
 
CSS
CSSCSS
CSS
 
Introduction to html
Introduction to htmlIntroduction to html
Introduction to html
 
Html presentation
Html presentationHtml presentation
Html presentation
 

Viewers also liked

How to develop app for facebook fan page
How to develop app for facebook fan pageHow to develop app for facebook fan page
How to develop app for facebook fan pageBunlong Van
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with RspecBunlong Van
 
scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891Bunlong Van
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented ProgrammingBunlong Van
 
Scrum methodology
Scrum methodologyScrum methodology
Scrum methodologyBunlong Van
 
Real time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming languageReal time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming languageBunlong Van
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic JavascriptBunlong Van
 

Viewers also liked (8)

How to develop app for facebook fan page
How to develop app for facebook fan pageHow to develop app for facebook fan page
How to develop app for facebook fan page
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891scrummethodology-151002092252-lva1-app6891
scrummethodology-151002092252-lva1-app6891
 
Javascript Object Oriented Programming
Javascript Object Oriented ProgrammingJavascript Object Oriented Programming
Javascript Object Oriented Programming
 
Why ruby?
Why ruby?Why ruby?
Why ruby?
 
Scrum methodology
Scrum methodologyScrum methodology
Scrum methodology
 
Real time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming languageReal time web and mobile application with Erlang & Ruby programming language
Real time web and mobile application with Erlang & Ruby programming language
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 

Similar to Javascript dom event

JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & eventBorey Lim
 
Xml part 6
Xml part 6Xml part 6
Xml part 6NOHA AW
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxkarthiksmart21
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handlingsmitha273566
 
Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Thinkful
 
The Road to Native Web Components
The Road to Native Web ComponentsThe Road to Native Web Components
The Road to Native Web ComponentsMike North
 
Michael North "The Road to Native Web Components"
Michael North "The Road to Native Web Components"Michael North "The Road to Native Web Components"
Michael North "The Road to Native Web Components"Fwdays
 
WEB PROGRAMMING ANALYSIS
WEB PROGRAMMING ANALYSISWEB PROGRAMMING ANALYSIS
WEB PROGRAMMING ANALYSISsweetysweety8
 
Web components
Web componentsWeb components
Web componentsNoam Kfir
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy stepsprince Loffar
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScriptLilia Sfaxi
 
Website designing company in faridabad
Website designing company in faridabadWebsite designing company in faridabad
Website designing company in faridabadCss Founder
 
Javascript part 2 DOM.pptx
Javascript part 2 DOM.pptxJavascript part 2 DOM.pptx
Javascript part 2 DOM.pptxdeepa339830
 
Java Script and HTML.
Java Script and HTML.Java Script and HTML.
Java Script and HTML.Akshat Vig
 
Basic web security model
Basic web security modelBasic web security model
Basic web security modelG Prachi
 

Similar to Javascript dom event (20)

JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & event
 
Xml part 6
Xml part 6Xml part 6
Xml part 6
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)
 
Part 7
Part 7Part 7
Part 7
 
The Road to Native Web Components
The Road to Native Web ComponentsThe Road to Native Web Components
The Road to Native Web Components
 
Michael North "The Road to Native Web Components"
Michael North "The Road to Native Web Components"Michael North "The Road to Native Web Components"
Michael North "The Road to Native Web Components"
 
lect9
lect9lect9
lect9
 
lect9
lect9lect9
lect9
 
WEB PROGRAMMING ANALYSIS
WEB PROGRAMMING ANALYSISWEB PROGRAMMING ANALYSIS
WEB PROGRAMMING ANALYSIS
 
Web components
Web componentsWeb components
Web components
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
Website designing company in faridabad
Website designing company in faridabadWebsite designing company in faridabad
Website designing company in faridabad
 
JavaScript and DOM
JavaScript and DOMJavaScript and DOM
JavaScript and DOM
 
Javascript part 2 DOM.pptx
Javascript part 2 DOM.pptxJavascript part 2 DOM.pptx
Javascript part 2 DOM.pptx
 
Java Script and HTML.
Java Script and HTML.Java Script and HTML.
Java Script and HTML.
 
Basic web security model
Basic web security modelBasic web security model
Basic web security model
 
03DOM.ppt
03DOM.ppt03DOM.ppt
03DOM.ppt
 

Recently uploaded

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 

Recently uploaded (20)

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 

Javascript dom event

  • 1. Click to add Text Javascript Presented by Bunlong VAN http://geekhmer.github.io DOM & Event
  • 3. DOM •DOM (Document Object Modeling) •Retrieving Nodes • document.getElementById(id) • document.getElementsByName(name) • Node.getElementsByTagName(tagName)
  • 4. Document Tree Structure <html> <body> <h1>Heading 1</h1> <p>Paragraph.</p> <h2>Heading 2</h2> <p>Paragraph.</p> </body> </html> #text H1 P H2 BODY #document HTML HEAD #text P #text #text
  • 5. child, sibling, parent BODY H1 P H2 #text #text P #text #text lastChild lastChild lastChild lastChild lastChild firstChild firstChild firstChild firstChild firstChild
  • 6. child, sibling, parent (Con.) BODY H1 P H2 #text #text P #text #text lastChild lastChild lastChild lastChild lastChild firstChild firstChild firstChild firstChild firstChild nextSibling nextSibling nextSibling previousSibling previousSibling previousSibling
  • 7. child, sibling, parent (Con.) H1 #text lastChild P H2 P #text #text #text lastChild lastChild lastChild lastChild firstChild firstChild firstChild firstChild firstChild nextSibling nextSibling nextSibling previousSibling previousSibling previousSibling parentNode parentNode parentNode parentNode parentNode BODY
  • 8. child, sibling, parent (Con.) BODY H1 P H2 #text #text P #text #text firstChild firstChild firstChild firstChild firstChild nextSibling nextSibling nextSibling
  • 9. Child Nodes BODY 0 1 2 P childNodes H1 H2 P 3
  • 10. Style •node.style.stylename •background-color •border-radius •font-size •list-style-type •word-spacing •z-index •backgroundColor •borderRadius •fontSize •listStyleType •wordSpacing •zIndex
  • 11. Making Elements document.createElement(tagName); document.createTextNode(text); node.cloneNode(); // clone an individual element node.cloneNode(true); // clone an element and all of its des
  • 12. Linking Ements node.appendChild(new); node.insertBefore(new, sibling); node.replaceChild(new, old); old.parentNode.replaceChild(new, old);
  • 13. Removing Elements node.removeChild(old); // It returns the node old.parentNode.removeChild(old);
  • 14. innerHTML •The W3C standard does not provide access to the HTML parser •All browsers implement Microsoft's innerHTML property •node.innerHTML = “<p>this is text</p>”;
  • 15. Which Way is Better? •It is better to build or clone elements and append them to the document? •Or is it better to complile an HTML text and use innerHTML to realize it? •Favor clean code and easy maintenance •Favor performance only in extreme cases
  • 16. Event •The browser has an event-driven, single-threaded, asynchronous programming model •Events are targeted to particular nodes •Event cause the invocation of event handler functions
  • 17. Mouse Event The target is the topmost (z-index) node containing the cursor: •click •dblclick •mousedown •mousemove •mouseout •mouseover •mouseup
  • 18. Input Event The target is the node having focus: •blur •change •focus •keydown •keyup •reset •submit
  • 19. Event Handler •Classic – node[“on” + type] = handler; •Microsoft – node.attachEvent(“on” + type, handler); •W3C – node.addEventListener(type, handler, false); var handler = function(e) { e = e || event; var target = e.target || e.srcElement; … }
  • 20. Bubbling Bubbling means that the event is given to the target, and then its parent, and so on until the event is canceled.
  • 21. Why Bubble? •Suppose you have 100 draggable objects •You could attach 100 sets of event handlers to those objects •Or you could attach one set of event handlers to the container of the 100 objects
  • 22. Cancel Bubbling •Cancel bubbling to keep the parent nodes from seeing th event: e.cancelBubble = true; If(e.stopPropagation) { e.stopPropagation(); }
  • 23. Prevent Default Action •An event handler can prevent a browser action associated with the event (such as submitting a form): e.returnValue = false; If(e.preventDefault) { e.preventDefault(); } Return false;