CGS 3066: Web Programming and Design Spring 2017

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 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.
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.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
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.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
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.
Manipulating the DOM CST 200 – JavaScript 3 –
JavaScript – The DOM JavaScript is object based The browser is object based – We can access the browser's objects in the same way we did JavaScript's Two.
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.
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
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.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
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.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
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.
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:
Introduction to JavaScript DOM Instructor: Sergey Goldman.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
Advanced Topics in Concurrency and Reactive Programming: HTML, CSS and DOM Trees Majeed Kassis.
THE DOM.
Introduction to.
Programming Web Pages with JavaScript
Unit 4 Representing Web Data: XML
>> JavaScript: Document Object Model
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unit M Programming Web Pages with
CHAPTER 5 DOCUMENT OBJECT MODEL
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
Applied Online Programming
CS 371 Web Application Programming
CGS 3066: Web Programming and Design Spring 2016
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.
Working with Dynamic Content and Styles
Document Object Model (DOM): Objects and Collections
LING 408/508: Computational Techniques for Linguists
2017, Fall Pusan National University Ki-Joune Li
Javascript and JQuery SRM DSC.
Murach's JavaScript and jQuery (3rd Ed.)
Presentation transcript:

CGS 3066: Web Programming and Design Spring 2017 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 CamelCase 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 (Document Object Model) Nodes Source: http://www.w3schools.com/js/js_htmldom_navigation.asp

Adding HTML Elements /*Create a new element and store it */ // Create a new element and store it in a variable. var newEl = document.createElement('li'); // // // Create a text node and store it in a variable. var newText = document.createTextNode('php'); // // Attach the new text node to the new element. newEl.appendChild(newText); // Find the position where the new element should be added. var position = document.getElementsByTagName('ul')[0]; // // Insert the new element into its position. position.appendChild(newEl);

Removing Existing HTML Elements /* Remove an element */ // Store the element to be removed in a variable. var removeEl = document.getElementsByTagName('li')[3]; // // // Find the element which contains the element to be removed. var containerEl = document.getElementsByTagName('ul')[0]; // // Remove the element. containerEl.removeChild(removeEl);

Attributes of HTML elements /* Get attributes of an element */ var firstItem = document.getElementById('one'); // Get first list item if (firstItem.hasAttribute('class')) {          // If it has class attribute     var attr = firstItem.getAttribute('class');   // Get the attribute   // Add the value of the attribute after the list     var el = document.getElementById('scriptResults');     el.innerHTML = '<p>The first item has a class name: ' + attr + '</p>'; }

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"