CGS 3066: Web Programming and Design Spring 2016

Slides:



Advertisements
Similar presentations
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.
Advertisements

Molecular Biomedical Informatics Web Programming 1.
getElementById() document.getElementById(“idName").innerHTML = “Any valid content"; getElementById is a method innerHTML is a property.
The DOM tree CS The DOM tree CS380 2 Types of DOM nodes  element nodes (HTML tag)  can have children and/or attributes  text nodes (text in.
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Page Elements © Copyright 2014, Fred McClurg All Rights Reserved.
Lesson 12- Unit L Programming Web Pages with JavaScript.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I HTML DOM part I Jongwook.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
XML DOM Week 11 Web site:
DOM Dr. Reda Salama. Back to the HTML DOM Once we get a response back from the server, we probably want to update our HTML page The HTML page itself is.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
CSCI 3100 Tutorial 5 JavaScript & Ajax Jichuan Zeng Department of Computer Science and Engineering The Chinese University of Hong.
Java Script and the DOM DOM stands for: –The Document Object Model When a browser reads a web page, it converts it’s contents from HTML into a hierarchical.
Advanced Topics in Concurrency and Reactive Programming: HTML, CSS and DOM Trees Majeed Kassis.
THE DOM.
Introduction to JavaScript DOM and Events
Unit 4 Representing Web Data: XML
>> JavaScript: Document Object Model
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
CHAPTER 5 DOCUMENT OBJECT MODEL
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
CGS 3066: Web Programming and Design Spring 2017
Applied Online Programming
CS 371 Web Application Programming
Intro to JavaScript CS 1150 Spring 2017.
CGS 3066: Web Programming and Design Spring 2017
JavaScript Using JavaScript.
Document Object Model (DOM): Objects and Collections
Chapter 7 Representing Web Data: XML
Week 11 Web site: XML DOM Week 11 Web site:
Processing XML.
Introduction to jQuery
Document Object Model (DOM): Objects and Collections
LING 408/508: Computational Techniques for Linguists
2017, Fall Pusan National University Ki-Joune Li
CSc 337 Lecture 15: Review.
That detail is only sort of important That detail is only sort of important. What is important is that you have all of these HTML elements floating.
Chengyu Sun California State University, Los Angeles
Murach's JavaScript and jQuery (3rd Ed.)
JavaScript and the DOM MIS 2402 Maxwell Furman Department of MIS
Presentation transcript:

CGS 3066: Web Programming and Design Spring 2016 DOM manipulation using JavaScript

getElementById() Returns an element object whose id matches with function argument: document.getElementById(“idName").innerHTML = “Any valid content"; getElementById is a method applied to the document object innerHTML is a property of the element object returned by document.getElementById(“idName")

getElementsByClassName() Returns an array of objects whose class attribute match to the function argument var elements= document.getElementsByClassName(“className"); //Example: update all matching element contents to “foo” for(var i=0;i<elements.length;i++) { elements[i].innerHTML=“foo"; } //Example: update the first matching element content to “foo” document.getElementsByClassName(“className")[0].innerHTML=“foo”;

getElementsByTagName() Returns all elements in the document with the specified tag name as an array var z=document.getElementsByTagName("div"); for(var i=0;i<z.length;i++) { z[i].innerHTML=“Updated content“; }

Changing CSS/Style attributes Syntax: element.style.propertyname=value; DOM Property names similar to CSS property names, but in CamesCase and have no hiphens var z=document.getElementsByTagName("div"); for(var i=0;i<z.length;i++) { //changing CSS ‘background-color’ property z[i].style.backgroundColor="red"; //changing CSS ‘border-radius’ property z[i].style.borderRadius="10px"; }

Change attribute of an HTML element Use either element.attribute= value; Or, element.setAttribute(attribute,value)

Image src Attribute Change <html> … <img id="img1" src=“image1.png“ onmouseover="changeI()"> <script> function changeI() { var x=document.getElementById("img1"); x.src=“newimage.png"; } </script> </html>

DOM Nodes Source: http://www.w3schools.com/js/js_htmldom_navigation.asp

Creating New HTML Elements <div id="div1"> <p id="p1">This is a paragraph.</p> <p id="p2">This is another paragraph.</p> </div> <script> var para = document.createElement("p"); var node = document.createTextNode("This is new."); para.appendChild(node); var element = document.getElementById("div1"); element.appendChild(para); </script>

Creating new HTML Elements - insertBefore() <div id="div1"> <p id="p1">This is a paragraph.</p> <p id="p2">This is another paragraph.</p> </div> <script> var para = document.createElement("p"); var node = document.createTextNode("This is new."); para.appendChild(node); var element = document.getElementById("div1"); var child = document.getElementById("p1"); element.insertBefore(para,child); </script>

Removing Existing HTML Elements <div id="div1"> <p id="p1">This is a paragraph.</p> <p id="p2">This is another paragraph.</p> </div> <script> var parent = document.getElementById("div1"); var child = document.getElementById("p1"); parent.removeChild(child); </script>

Replacing HTML Elements <div id="div1"> <p id="p1">This is a paragraph.</p> <p id="p2">This is another paragraph.</p> </div> <script> var para = document.createElement("p"); var node = document.createTextNode("This is new."); para.appendChild(node); var parent = document.getElementById("div1"); var child = document.getElementById("p1"); parent.replaceChild(para,child); </script>

parentNode and childNodes Each Node in the DOM tree (except the root, <html> ) has a parentNode property May be used to access the parent Node Object childNodes property returns a live collection of nodes: any change to the DOM <ul id="parent"> <li id=«child1">Child1</li> <li>Child2</li> </ul> console.log(document.getElementById("child1").parentNode.id); //prints ‘parent’ var parent = document.getElementById('parent'); var child_nodes = parent.childNodes; console.log(child_nodes.length); // should output "2" parent.appendChild(document.createElement(‘li')); console.log(child_nodes.length); // should output "3"