Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3066: Web Programming and Design Spring 2017

Similar presentations


Presentation on theme: "CGS 3066: Web Programming and Design Spring 2017"— Presentation transcript:

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

2 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")

3 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”;

4 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“; }

5 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"; }

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

7 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>

8 DOM (Document Object Model) Nodes
Source:

9 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);

10 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);

11 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>'; }

12 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"


Download ppt "CGS 3066: Web Programming and Design Spring 2017"

Similar presentations


Ads by Google