SlideShare a Scribd company logo
HFJS – Book
Review Ch8
Brady Cheng
Agenda
 GetHTML Element
 Update the HTML Element
 DOM(Document Object Model)
Get HTML element
 getElementByID
 getElementByTagName
<body>
       <img id=“img1” src=“img1.png” alt=“first image”/>
       <img id=“img2” src=“img2.png” alt=“second image”/>
       <img id=“img3” src=“img3.png” alt=“third image”/>
</body>


         document.getElementById(“img1”);
         document.getElementByTagName(“img”)[0];
Update the HTML Element
     Use   innerHTML property to update
<head>
<script type="text/javascript">
  function updateText()
  {
      document.getElementById("text").innerHTML = "Hello, JS world!";
  }
</script>
</head>
<body>
    <p id="text" onclick="updateText();"> Hello, world!</p>
</body>
               Output: Hello, world! -> Hello, JS world!
DOM
     DOM(document   object model) is a
      concept to organize our html as a tree-
      like structure
                                       document

                                            html
<head> …</head>
<body>                               head          body
   <p id="text"> Hello, world!</p>
</body>                                             p


                                               Hello, world!
DOM
 Every block is a node
 Upper node is the parent of lower node
 Two types of node            document
    Element
                                    html
    Text
       Parent of head/body   head          body


          Child of html                     p


                                       Hello, world!
DOM
 Some    properties to describe nodes
    nodeValue
    nodeType
                  document
    childNode
                    html
    fisrtChild
    lastChild head        body


                             p
      Id=textId         Hello, world
                              !
alert(document.getElementById("textId").firstChild.nodeValue);
DOM
 Some   functions to operate nodes
    removeChild();
    createTextNode();
    appendChild();


 We use DOM to implement the
 aforementioned updateText() function
DOM
 Use   innerHTML
document.getElementById("text").innerHTML = "Hello, JS world!";

 Use   DOM
while(node.firstChild)
        node.removeChild(node.firstChild);

var new_node = document.createTextNode("Hello, JS world!");
node.appendChild(new_node);



         Output: Hello, world! -> Hello, JS world!

More Related Content

PPTX
MongoDB Aug2010 SF Meetup
PPTX
บท7
PPT
JavaScript
PPTX
Document Object Model
PDF
Java script how to
PPTX
Ch4(saving state with cookies and query strings)
PPT
JavaScript & Dom Manipulation
MongoDB Aug2010 SF Meetup
บท7
JavaScript
Document Object Model
Java script how to
Ch4(saving state with cookies and query strings)
JavaScript & Dom Manipulation

What's hot (18)

PPT
Meetup#1: 10 reasons to fall in love with MongoDB
PPTX
Website slides
PDF
Mongophilly shell-2011-04-26
PDF
Mastering the MongoDB Shell
PPTX
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
PDF
Our local state, my, my - Understanding Perl variables
PDF
Mysql to mongo
PPT
Building Your First MongoDB App ~ Metadata Catalog
PPTX
33 meta
PDF
MongoDB a document store that won't let you down.
PDF
[WEB UI BASIC] JavaScript 1탄
PDF
Javascript and DOM
PPTX
Building a Location-based platform with MongoDB from Zero.
PPT
Zoho vs google doc[1]
PPTX
Lesson 206 11 oct13-1500-ay
PPT
PhpstudyTokyo MongoDB PHP CakePHP
PDF
Couch db skillsmatter-prognosql
PDF
Mongo Presentation by Metatagg Solutions
Meetup#1: 10 reasons to fall in love with MongoDB
Website slides
Mongophilly shell-2011-04-26
Mastering the MongoDB Shell
Internet and Web Technology (CLASS-7) [XML and AJAX] | NIC/NIELIT Web Technology
Our local state, my, my - Understanding Perl variables
Mysql to mongo
Building Your First MongoDB App ~ Metadata Catalog
33 meta
MongoDB a document store that won't let you down.
[WEB UI BASIC] JavaScript 1탄
Javascript and DOM
Building a Location-based platform with MongoDB from Zero.
Zoho vs google doc[1]
Lesson 206 11 oct13-1500-ay
PhpstudyTokyo MongoDB PHP CakePHP
Couch db skillsmatter-prognosql
Mongo Presentation by Metatagg Solutions
Ad

Viewers also liked (9)

PPTX
Javascript ch2
PPTX
RoR guide_p1
PPTX
Javascipt ch4 & ch5
PPTX
Javascript ch3
PPTX
Javascipt ch1
PPTX
Ruby introduction part1
PPTX
Javascript ch6
PPTX
Javascript ch7
PPTX
design pattern overview
Javascript ch2
RoR guide_p1
Javascipt ch4 & ch5
Javascript ch3
Javascipt ch1
Ruby introduction part1
Javascript ch6
Javascript ch7
design pattern overview
Ad

Similar to Javascript ch8 (20)

PPTX
Dom date and objects and event handling
PDF
PDF
PPTX
Web technologies-course 09.pptx
PDF
Getting Started with DOM
PPTX
DOM and Events
PDF
Web 6 | JavaScript DOM
PDF
Introduction to Javascript
PPT
6867389.ppt
PPT
6867389 (1).ppt
PPT
6867389.ppt
PDF
Intro to JavaScript
PPTX
Web____Dev_____Bootcamp____Presentationn
PPT
Javascript dom event
PPTX
Introduction to the DOM
PPT
DOM Quick Overview
PPTX
Unit5_Web_Updvvgxsvjbffcvvgbjifszated.pptx
PDF
Java Script and HTML.
PPTX
HelloWorld
PPTX
Hello world
Dom date and objects and event handling
Web technologies-course 09.pptx
Getting Started with DOM
DOM and Events
Web 6 | JavaScript DOM
Introduction to Javascript
6867389.ppt
6867389 (1).ppt
6867389.ppt
Intro to JavaScript
Web____Dev_____Bootcamp____Presentationn
Javascript dom event
Introduction to the DOM
DOM Quick Overview
Unit5_Web_Updvvgxsvjbffcvvgbjifszated.pptx
Java Script and HTML.
HelloWorld
Hello world

Javascript ch8

  • 1. HFJS – Book Review Ch8 Brady Cheng
  • 2. Agenda  GetHTML Element  Update the HTML Element  DOM(Document Object Model)
  • 3. Get HTML element  getElementByID  getElementByTagName <body> <img id=“img1” src=“img1.png” alt=“first image”/> <img id=“img2” src=“img2.png” alt=“second image”/> <img id=“img3” src=“img3.png” alt=“third image”/> </body> document.getElementById(“img1”); document.getElementByTagName(“img”)[0];
  • 4. Update the HTML Element  Use innerHTML property to update <head> <script type="text/javascript"> function updateText() { document.getElementById("text").innerHTML = "Hello, JS world!"; } </script> </head> <body> <p id="text" onclick="updateText();"> Hello, world!</p> </body> Output: Hello, world! -> Hello, JS world!
  • 5. DOM  DOM(document object model) is a concept to organize our html as a tree- like structure document html <head> …</head> <body> head body <p id="text"> Hello, world!</p> </body> p Hello, world!
  • 6. DOM  Every block is a node  Upper node is the parent of lower node  Two types of node document  Element html  Text Parent of head/body head body Child of html p Hello, world!
  • 7. DOM  Some properties to describe nodes  nodeValue  nodeType document  childNode html  fisrtChild  lastChild head body p Id=textId Hello, world ! alert(document.getElementById("textId").firstChild.nodeValue);
  • 8. DOM  Some functions to operate nodes  removeChild();  createTextNode();  appendChild();  We use DOM to implement the aforementioned updateText() function
  • 9. DOM  Use innerHTML document.getElementById("text").innerHTML = "Hello, JS world!";  Use DOM while(node.firstChild) node.removeChild(node.firstChild); var new_node = document.createTextNode("Hello, JS world!"); node.appendChild(new_node); Output: Hello, world! -> Hello, JS world!