SlideShare a Scribd company logo
1 of 10
HTML DOM Part-II
Create Node
• We can create a node in JavaScript
dynamically.
• There are 3 types of Node
– Comment
– Element
– Text Node
• We can also create an attribute for a tag.
Create Comment Node
• createComment(string)
– Creates a text comment of the form
<!- - string - ->, where string is the comment
content.
For ex:
comNode=document.createComment(“just a
comment”);
Create Element Tag
• createElement(tagName)
– Creates an element of type tag, where
tagName is the name of tag.
For ex:
newTagId = document.createElement(“h1”)
Create Text Node
• createTextNode(string)
– Creates a text node containing string.
For ex:
newText = document.createTextNode(“Some
new text”);
Create attribute
• We can dynamically create an attribute.
• createAttribute(attributeName)
– Creates an attribute with attribute name as
argument.
– We will see an example of this later.
Join all stuff
newNode = document.createElement(“p”);
newText = document.createTextNode(“new text”);
If I write this code in JavaScript, It wont display
anything on browser.
I need to join newNode and newText and insert
them in the web page document to view it.
Insert a new Element
• Slide 3, 4, 5 taught us how to create a new
Element.
• Slide 7 told us that, we need to insert new
element to view it.
• This slide presents 2 methods of inserting
new element in document web page.
– insertBefore (newChild,referenceChild)
– appendChild (newChild)
Insert and Append
• parentNode.insertBefore(newChild,referenceChild)
– Inserts a new node as a child of parentNode.
– newChild is inserted node referenceChild.
– Please Note that, referenceChild is a child of parentNode. We want to insert newChild as
a cahild of parentNode but before referenceChild
• parentNode.appendChild(newChild)
– Insert a new node as a child of parentNode.
– newChild is inserted as the last Node or last Children of parentNode.
For ex:
<body>
<h1>Dom Insert and Append</h1>
<hr>
<div id=“outerDiv”style=“background-color:#66ff00;”>
<div id=“innerDiv” style=“background-color:#ffcc00”> </div>
</div>
<form id=“form1” action=“#” method=“get”>
<input type=“text” id=“field1”>
<input type=“button” value=“Insert Before” onClick=insertBefore()>
</form>
</body>
Example Continue…….
Insert the code in the <head>
<script language=“JavaScript”>
function insertBefore()
{
textId = document.getElementById(“field1”);
refChild = document.getElementById(“innerDiv”);
var newPara = document.createElement(“p”);
var newText = document.createText(textId.value);
// insert a new Node as child of newPara.
newPara.appendChild(newText);
if(refChild.parentNode) // does node innerDiv has parent Node
{
parentId = refChild.parentNode; // get the Id of parent Node
//insert new node, i.e. newPara, as a child of outerDiv but before innerDiv.
parentId.insertBefore(newPara,refChild);
}
}
</script>

More Related Content

Similar to dom.ppt

9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript WorkshopPamela Fox
 
Java Script and HTML.
Java Script and HTML.Java Script and HTML.
Java Script and HTML.Akshat Vig
 
Page object from the ground up.ppt
Page object from the ground up.pptPage object from the ground up.ppt
Page object from the ground up.pptJoseph Beale
 
Page object from the ground up by Joe Beale
Page object from the ground up by Joe BealePage object from the ground up by Joe Beale
Page object from the ground up by Joe BealeQA or the Highway
 
Article writer process slideshow
Article writer process slideshowArticle writer process slideshow
Article writer process slideshowroxyboopy
 
Documentation For Tab Setup
Documentation For Tab SetupDocumentation For Tab Setup
Documentation For Tab Setupvkeeton
 
Devoxx 2014-webComponents
Devoxx 2014-webComponentsDevoxx 2014-webComponents
Devoxx 2014-webComponentsCyril Balit
 
descriptive programming
descriptive programmingdescriptive programming
descriptive programmingAnand Dhana
 
Abstracting functionality with centralised content
Abstracting functionality with centralised contentAbstracting functionality with centralised content
Abstracting functionality with centralised contentMichael Peacock
 
Polymer 2.0 codelab for extreme beginners
Polymer 2.0 codelab for extreme beginnersPolymer 2.0 codelab for extreme beginners
Polymer 2.0 codelab for extreme beginnersSylia Baraka
 

Similar to dom.ppt (20)

Introduction to js (cnt.)
Introduction to js (cnt.)Introduction to js (cnt.)
Introduction to js (cnt.)
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
 
HTML and CSS.pptx
HTML and CSS.pptxHTML and CSS.pptx
HTML and CSS.pptx
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
 
Java Script and HTML.
Java Script and HTML.Java Script and HTML.
Java Script and HTML.
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
BYOWHC823
BYOWHC823BYOWHC823
BYOWHC823
 
Page object from the ground up.ppt
Page object from the ground up.pptPage object from the ground up.ppt
Page object from the ground up.ppt
 
Page object from the ground up by Joe Beale
Page object from the ground up by Joe BealePage object from the ground up by Joe Beale
Page object from the ground up by Joe Beale
 
MMI-402 - Lecture 1
MMI-402 - Lecture 1MMI-402 - Lecture 1
MMI-402 - Lecture 1
 
Article writer process slideshow
Article writer process slideshowArticle writer process slideshow
Article writer process slideshow
 
DOM Quick Overview
DOM Quick OverviewDOM Quick Overview
DOM Quick Overview
 
Documentation For Tab Setup
Documentation For Tab SetupDocumentation For Tab Setup
Documentation For Tab Setup
 
Devoxx 2014-webComponents
Devoxx 2014-webComponentsDevoxx 2014-webComponents
Devoxx 2014-webComponents
 
descriptive programming
descriptive programmingdescriptive programming
descriptive programming
 
Abstracting functionality with centralised content
Abstracting functionality with centralised contentAbstracting functionality with centralised content
Abstracting functionality with centralised content
 
Javascript.ppt
Javascript.pptJavascript.ppt
Javascript.ppt
 
Java script
Java scriptJava script
Java script
 
Polymer 2.0 codelab for extreme beginners
Polymer 2.0 codelab for extreme beginnersPolymer 2.0 codelab for extreme beginners
Polymer 2.0 codelab for extreme beginners
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

dom.ppt

  • 2. Create Node • We can create a node in JavaScript dynamically. • There are 3 types of Node – Comment – Element – Text Node • We can also create an attribute for a tag.
  • 3. Create Comment Node • createComment(string) – Creates a text comment of the form <!- - string - ->, where string is the comment content. For ex: comNode=document.createComment(“just a comment”);
  • 4. Create Element Tag • createElement(tagName) – Creates an element of type tag, where tagName is the name of tag. For ex: newTagId = document.createElement(“h1”)
  • 5. Create Text Node • createTextNode(string) – Creates a text node containing string. For ex: newText = document.createTextNode(“Some new text”);
  • 6. Create attribute • We can dynamically create an attribute. • createAttribute(attributeName) – Creates an attribute with attribute name as argument. – We will see an example of this later.
  • 7. Join all stuff newNode = document.createElement(“p”); newText = document.createTextNode(“new text”); If I write this code in JavaScript, It wont display anything on browser. I need to join newNode and newText and insert them in the web page document to view it.
  • 8. Insert a new Element • Slide 3, 4, 5 taught us how to create a new Element. • Slide 7 told us that, we need to insert new element to view it. • This slide presents 2 methods of inserting new element in document web page. – insertBefore (newChild,referenceChild) – appendChild (newChild)
  • 9. Insert and Append • parentNode.insertBefore(newChild,referenceChild) – Inserts a new node as a child of parentNode. – newChild is inserted node referenceChild. – Please Note that, referenceChild is a child of parentNode. We want to insert newChild as a cahild of parentNode but before referenceChild • parentNode.appendChild(newChild) – Insert a new node as a child of parentNode. – newChild is inserted as the last Node or last Children of parentNode. For ex: <body> <h1>Dom Insert and Append</h1> <hr> <div id=“outerDiv”style=“background-color:#66ff00;”> <div id=“innerDiv” style=“background-color:#ffcc00”> </div> </div> <form id=“form1” action=“#” method=“get”> <input type=“text” id=“field1”> <input type=“button” value=“Insert Before” onClick=insertBefore()> </form> </body>
  • 10. Example Continue……. Insert the code in the <head> <script language=“JavaScript”> function insertBefore() { textId = document.getElementById(“field1”); refChild = document.getElementById(“innerDiv”); var newPara = document.createElement(“p”); var newText = document.createText(textId.value); // insert a new Node as child of newPara. newPara.appendChild(newText); if(refChild.parentNode) // does node innerDiv has parent Node { parentId = refChild.parentNode; // get the Id of parent Node //insert new node, i.e. newPara, as a child of outerDiv but before innerDiv. parentId.insertBefore(newPara,refChild); } } </script>