SlideShare a Scribd company logo
Document	
  Object	
  Model	
  and	
  
JavaScript	
  
Jussi	
  Pohjolainen	
  
Tampere	
  University	
  of	
  Applied	
  Sciences	
  
W3C	
  DOM	
  
•  DOM	
  –	
  Document	
  Object	
  Model	
  –	
  cross-­‐
plaDorm	
  and	
  language-­‐independent	
  
convenFon	
  for	
  interacFng	
  with	
  objects	
  in	
  
HTML	
  and	
  in	
  XML.	
  
•  With	
  DOM	
  you	
  can	
  manipulate	
  html/xml	
  
document!	
  Dynamic	
  html!	
  
•  Public	
  interface	
  available:	
  hPp://www.w3.org/
DOM/DOMTR	
  	
  
W3C	
  DOM	
  Levels	
  
•  (	
  DOM	
  Level	
  0	
  and	
  Intermediate	
  DOM	
  )	
  
–  Not	
  W3C	
  Standards,	
  used	
  in	
  Netscape	
  2	
  (Level	
  0)	
  and	
  
Netscape	
  4	
  (Intermediate	
  DOM)	
  	
  
•  DOM	
  Level	
  1	
  
–  1998:	
  Ability	
  to	
  change	
  enFre	
  HTML	
  or	
  XML	
  document	
  
•  DOM	
  Level	
  2	
  
–  2001:	
  Introduces	
  “getElementById”	
  funcFon,	
  event	
  
model	
  and	
  support	
  for	
  XML	
  namespaces	
  
•  DOM	
  Level	
  3	
  
–  2004:	
  XPath,	
  keyboard	
  event	
  handling	
  
Reality	
  
See	
  
hPp://www.quirksmode.org/dom/w3c_core.html	
  
DOM	
  Programming	
  Intro	
  
•  DOM	
  is	
  a	
  model	
  that	
  describes	
  how	
  all	
  
elements	
  in	
  an	
  html	
  page	
  are	
  related	
  to	
  
topmost	
  structure:	
  document	
  itself	
  
•  Can	
  influence	
  the	
  document	
  
– See:	
  hPp://www.w3.org/TR/REC-­‐DOM-­‐Level-­‐1/
introducFon.html	
  
•  Possible	
  to	
  read,	
  modify	
  and	
  delete	
  tags	
  
Node	
  
•  In	
  DOM,	
  each	
  object	
  is	
  Node	
  
•  In	
  this	
  
– <p>Hello</p>	
  
•  You	
  have	
  two	
  nodes	
  	
  
– 1)	
  element	
  node	
  -­‐	
  p	
  	
  
– 2)	
  text	
  node	
  -­‐	
  "Hello"	
  
•  Text	
  node	
  is	
  child	
  node	
  of	
  p	
  element.	
  P	
  
element	
  is	
  parent	
  node	
  of	
  the	
  text	
  node	
  
Node	
  Example	
  
<p>This is a <strong>paragraph</strong></p>
<p>
|
--------------
| |
This is a <strong>
|
|
paragraph
APribute	
  Node	
  
<a href=“http://www.tamk.fi”>TAMK</a>
<a> -----------------
| |
TAMK href
|
http://www.tamk.fi
Different	
  Nodes	
  
•  Element	
  node:	
  p,	
  img,	
  a	
  
•  Text	
  node:	
  sometext	
  
•  APribute	
  node:	
  src	
  
DOM	
  Level	
  1:	
  To	
  Access	
  DOM	
  tree	
  
•  X	
  can	
  be	
  some	
  node	
  
– var parent = x.parentNode;
– var firstchild = x.childNodes[0];
•  How	
  to	
  get	
  reference	
  to	
  x?	
  
Document	
  object	
  
Access	
  
var title =
document.firstChild.firstChild.lastChild;
// document.html.head.title
var title =
document.firstChild.childNodes[0].childNodes[
0];
Gelng	
  Element	
  Easier	
  Way	
  
var x =
document.getElementsByTagName(‘strong')[0]
var x = document.getElementById('hereweare');
	
  
Changing	
  the	
  Node	
  
// <a href=“” id=“someId”>Link</p>
var x =
document.getElementById(’someId');
x.firstChild.nodeValue = “Hello!”;
x.setAttribute(“href”, “http:/…”);
	
  
Inner	
  HTML	
  
// <a href=“” id=“someId”>Link</p>
var x = document.getElementById(’someId');
x.innerHTML = “Hello!”;
// innerHTML is NOT W3C Standard and it’s
// slower…
CreaFng	
  and	
  Removing	
  Nodes	
  
var x = document.createElement(’hr');
document.getElementById('inserthrhere').appendChild(x);
var node = document.getElementById('inserthrhere')
node.removeChild(node.childNodes[0]);
var x = document.createTextNode(’SomeText');
document.getElementById('hereweare').appendChild(x);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function change()
{
// Get list of ALL <h1> - elements
var listOfHeading1 = window.document.getElementsByTagName("h1");
// Find the first <h1> - element in the list
var heading1 = listOfHeading1[0];
// Get the child - element of the first <h1> - element (Text)
var text = heading1.firstChild;
// Replace the text
text.data = "Hello from JS!";
}
//]]>
</script>
</head>
<body>
<h1>Title</h1>
<input type="submit" onClick="change();" value="click!"/>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function change()
{
// Reference to the table - element
var table = document.getElementById("mytable");
var tr = document.createElement("tr"); // <tr>
var td1 = document.createElement("td"); // <td>
var td1Text = document.createTextNode("New Cell"); // "New Cell"
td1.appendChild(td1Text); // <td>New Cell</td>
var td2 = document.createElement("td"); // <td>
var td2Text = document.createTextNode("New Cell"); // "New Cell"
td2.appendChild(td2Text); // <td>New Cell</td>
tr.appendChild(td1);
tr.appendChild(td2);
table.appendChild(tr);
}
//]]>
</script>
</head>
<body>
<table id="mytable" border="1">
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>
<input type="submit" onClick="change();" value="Add Row"/>
</body>
</html>

More Related Content

PPTX
Dom(document object model)
PPT
Document Object Model
PPTX
An Introduction to the DOM
PPTX
Document Object Model
PDF
JavaScript DOM & event
PPTX
PPTX
Document object model
Dom(document object model)
Document Object Model
An Introduction to the DOM
Document Object Model
JavaScript DOM & event
Document object model

What's hot (20)

PPTX
Introduction to the DOM
PPTX
INTRODUCTION TO DOM AND DOM TREE
PPTX
Document Object Model (DOM)
PPTX
Document object model(dom)
PPT
Document Object Model
PPT
introduction to the document object model- Dom chapter5
PPT
Xml dom & sax by bhavsingh maloth
PPTX
The Document Object Model
PPT
DOM Quick Overview
PPTX
Dom in javascript
PPTX
Introduction to DOM
PPTX
XML Document Object Model (DOM)
PPTX
Html dom & j query
PPT
Understanding XML DOM
PPTX
Understanding the dom by Benedict Ayiko
PPTX
INFT132 093 07 Document Object Model
PPTX
Web1O1 - Session 1
PPTX
Presentation
Introduction to the DOM
INTRODUCTION TO DOM AND DOM TREE
Document Object Model (DOM)
Document object model(dom)
Document Object Model
introduction to the document object model- Dom chapter5
Xml dom & sax by bhavsingh maloth
The Document Object Model
DOM Quick Overview
Dom in javascript
Introduction to DOM
XML Document Object Model (DOM)
Html dom & j query
Understanding XML DOM
Understanding the dom by Benedict Ayiko
INFT132 093 07 Document Object Model
Web1O1 - Session 1
Presentation
Ad

Viewers also liked (6)

PDF
Javascript and DOM
PPTX
JavaScript (without DOM)
PPTX
[SoftServe IT Academy] JavaScript Forms
PPT
JavaScript & Dom Manipulation
PPT
DOM ( Document Object Model )
PDF
JSP 빠르게 시작하기
Javascript and DOM
JavaScript (without DOM)
[SoftServe IT Academy] JavaScript Forms
JavaScript & Dom Manipulation
DOM ( Document Object Model )
JSP 빠르게 시작하기
Ad

Similar to JavaScript and DOM (20)

PDF
PDF
PPTX
WEB TECHNOLOGY Unit-4.pptx
PDF
HTML_DOM
PDF
Intro to JavaScript
PPTX
Basic web security model
PPTX
Web scraping with php
PPTX
Part 7
PPT
Javascript dom event
PPT
6867389 (1).ppt
PPT
6867389.ppt
PPT
6867389.ppt
PPTX
Dom date and objects and event handling
PDF
Modern Web UI - Web components
PPS
04 sm3 xml_xp_07
PDF
Intro to Web Standards
PPT
Document Object Model
PPTX
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
PPTX
Web technologies-course 09.pptx
PDF
Introduction to Javascript
WEB TECHNOLOGY Unit-4.pptx
HTML_DOM
Intro to JavaScript
Basic web security model
Web scraping with php
Part 7
Javascript dom event
6867389 (1).ppt
6867389.ppt
6867389.ppt
Dom date and objects and event handling
Modern Web UI - Web components
04 sm3 xml_xp_07
Intro to Web Standards
Document Object Model
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
Web technologies-course 09.pptx
Introduction to Javascript

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
Box2D and libGDX
PDF
libGDX: Screens, Fonts and Preferences
PDF
libGDX: Tiled Maps
PDF
libGDX: User Input and Frame by Frame Animation
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
libGDX: User Input
PDF
Implementing a Simple Game using libGDX
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Creating Games for Asha - platform
PDF
Intro to Asha UI
Moved to Speakerdeck
Java Web Services
Box2D and libGDX
libGDX: Screens, Fonts and Preferences
libGDX: Tiled Maps
libGDX: User Input and Frame by Frame Animation
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
libGDX: User Input
Implementing a Simple Game using libGDX
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Games for Asha - platform
Intro to Asha UI

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Machine Learning_overview_presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation theory and applications.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
Machine Learning_overview_presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation theory and applications.pdf
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectroscopy.pptx food analysis technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx

JavaScript and DOM

  • 1. Document  Object  Model  and   JavaScript   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2. W3C  DOM   •  DOM  –  Document  Object  Model  –  cross-­‐ plaDorm  and  language-­‐independent   convenFon  for  interacFng  with  objects  in   HTML  and  in  XML.   •  With  DOM  you  can  manipulate  html/xml   document!  Dynamic  html!   •  Public  interface  available:  hPp://www.w3.org/ DOM/DOMTR    
  • 3. W3C  DOM  Levels   •  (  DOM  Level  0  and  Intermediate  DOM  )   –  Not  W3C  Standards,  used  in  Netscape  2  (Level  0)  and   Netscape  4  (Intermediate  DOM)     •  DOM  Level  1   –  1998:  Ability  to  change  enFre  HTML  or  XML  document   •  DOM  Level  2   –  2001:  Introduces  “getElementById”  funcFon,  event   model  and  support  for  XML  namespaces   •  DOM  Level  3   –  2004:  XPath,  keyboard  event  handling  
  • 5. DOM  Programming  Intro   •  DOM  is  a  model  that  describes  how  all   elements  in  an  html  page  are  related  to   topmost  structure:  document  itself   •  Can  influence  the  document   – See:  hPp://www.w3.org/TR/REC-­‐DOM-­‐Level-­‐1/ introducFon.html   •  Possible  to  read,  modify  and  delete  tags  
  • 6. Node   •  In  DOM,  each  object  is  Node   •  In  this   – <p>Hello</p>   •  You  have  two  nodes     – 1)  element  node  -­‐  p     – 2)  text  node  -­‐  "Hello"   •  Text  node  is  child  node  of  p  element.  P   element  is  parent  node  of  the  text  node  
  • 7. Node  Example   <p>This is a <strong>paragraph</strong></p> <p> | -------------- | | This is a <strong> | | paragraph
  • 8. APribute  Node   <a href=“http://www.tamk.fi”>TAMK</a> <a> ----------------- | | TAMK href | http://www.tamk.fi
  • 9. Different  Nodes   •  Element  node:  p,  img,  a   •  Text  node:  sometext   •  APribute  node:  src  
  • 10. DOM  Level  1:  To  Access  DOM  tree   •  X  can  be  some  node   – var parent = x.parentNode; – var firstchild = x.childNodes[0]; •  How  to  get  reference  to  x?  
  • 12. Access   var title = document.firstChild.firstChild.lastChild; // document.html.head.title var title = document.firstChild.childNodes[0].childNodes[ 0];
  • 13. Gelng  Element  Easier  Way   var x = document.getElementsByTagName(‘strong')[0] var x = document.getElementById('hereweare');  
  • 14. Changing  the  Node   // <a href=“” id=“someId”>Link</p> var x = document.getElementById(’someId'); x.firstChild.nodeValue = “Hello!”; x.setAttribute(“href”, “http:/…”);  
  • 15. Inner  HTML   // <a href=“” id=“someId”>Link</p> var x = document.getElementById(’someId'); x.innerHTML = “Hello!”; // innerHTML is NOT W3C Standard and it’s // slower…
  • 16. CreaFng  and  Removing  Nodes   var x = document.createElement(’hr'); document.getElementById('inserthrhere').appendChild(x); var node = document.getElementById('inserthrhere') node.removeChild(node.childNodes[0]); var x = document.createTextNode(’SomeText'); document.getElementById('hereweare').appendChild(x);
  • 17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function change() { // Get list of ALL <h1> - elements var listOfHeading1 = window.document.getElementsByTagName("h1"); // Find the first <h1> - element in the list var heading1 = listOfHeading1[0]; // Get the child - element of the first <h1> - element (Text) var text = heading1.firstChild; // Replace the text text.data = "Hello from JS!"; } //]]> </script> </head> <body> <h1>Title</h1> <input type="submit" onClick="change();" value="click!"/> </body> </html>
  • 18. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function change() { // Reference to the table - element var table = document.getElementById("mytable"); var tr = document.createElement("tr"); // <tr> var td1 = document.createElement("td"); // <td> var td1Text = document.createTextNode("New Cell"); // "New Cell" td1.appendChild(td1Text); // <td>New Cell</td> var td2 = document.createElement("td"); // <td> var td2Text = document.createTextNode("New Cell"); // "New Cell" td2.appendChild(td2Text); // <td>New Cell</td> tr.appendChild(td1); tr.appendChild(td2); table.appendChild(tr); } //]]> </script> </head> <body> <table id="mytable" border="1"> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> <tr><td>&nbsp;</td><td>&nbsp;</td></tr> </table> <input type="submit" onClick="change();" value="Add Row"/> </body> </html>

Editor's Notes

  • #5: See:http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(Document_Object_Model)Trident: IE 4.0 -&gt;Tasman: IE For MacPresto: Opera