SlideShare a Scribd company logo
1 of 34
Download to read offline
MedTech
Chp3- Client-Side JavaScript
DOM, Forms and HTML Manipulation
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 1
MedTech – Mediterranean Institute of Technology
CS-Web and Mobile Development
MedTech
MedTech
Window object
• Main entry point to all client-side JS features and APIs
• Represents a web browser window or frame
• Referred to with the identifier window
• It is the global object, and can be omitted
• Defines properties like:
• location (URL currently displayed)
• Ex: window.location = ”http://medtech.tn/”
navigates to the website of the institute
• alert() : displays a message in a dialog box
• setTimeout(): registers a function to be invoked after a specific amount of
time
• document: represents the content displayed in the window
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 2
Client-Side JavaScript
MedTech
DOM: Document Object Model
• W3C Standard
• Object-oriented model for representing HTML and XML documents
• The HTML DOM is a standard object model and programming
interface for HTML. It defines:
• The HTML elements as objects
• The properties of all HTML elements
• The methods to access all HTML elements
• The events for all HTML elements
• The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 3
Client-Side JavaScript
DOM: Document Object Model
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 4
Client-Side JavaScript
<html>
<head>
<title> My title</title>
</head>
<body>
<h1> A heading </h1>
<a href="link text">
and a link
</a>
</body>
</html>
MedTech
GOING THROUGH THE DOM ELEMENTS
Client-Side JavaScript
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 5
MedTech
DOM Programming Interface
• The HTML DOM can be accessed with JavaScript (and with other
programming languages).
• In the DOM, all HTML elements are defined as objects.
• The DOM Programming Interface is the set of properties and methods
of each object
• A property is a value that you can get or set (like changing the content of
an HTML element).
• A method is an action you can do (like add or deleting an HTML element).
• The top of the DOM tree is the object window
• The object document is a child element of window
• It represents the web page and enables to access the HTML element
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 6
Client-Side JavaScript
MedTech
DOM Types
• Node: The most generic type, all DOM
elements are nodes
• Element: HTML or XML Element
• HTML Element:
• An Element in an HTML DOM is an HTML
Element
• Represents the HTML elements of the
document: the tags
• All DOM elements are objects, thus
having their own methods and
attributes
• Some methods and attributes are
common to all DOM objects, as they
are all of the same type : Node
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 7
Client-Side JavaScript
Node
Element
HTMLElement
HTMLDivElement
MedTech
Elements Handling
• To access HTML elements using the DOM, several methods are offered
by the object document
• getElementById(): accesses an element via its id
• getElementByTagName(): gets all the elements of the given tag as an
array of objects
• getElementsByName(): returns an array of objects containing all the
elements whose attribute name is equal to the given argument
• querySelector()
• This function takes as a parameter a string representing a CSS selector
• Returns the first element that corresponds to this selector
• querySelectorAll()
• Returns an array of all the elements corresponding to the given selector
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 8
Client-Side JavaScript
MedTech
Elements Handling: getElementBy…
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 9
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title> My Test Page </title>
</head>
<body>
<div id="myMenu">
<div class="item">
<span> Menu 1</span>
<span> Menu 2</span>
</div>
<div class="Pub">
<span> Pub 1</span>
<span> Pub 2</span>
</div>
</div>
<div id="content">
<span>
Here is my content
</span>
</div>
A weird form to test
getElementsByName() :
<form name="myName">
<input type="password"
name= "myName">
<input>
<button name="myName"></button>
</form>
<script src="test.js"></script>
</body>
</html>
var getByid=
document.getElementById('myMenu');
var getElementByTagName =
document.getElementsByTagName("div");
var getElementByName =
document.getElementsByName("myName");
console.log("Result of getElementById : " +
getByid.textContent);
console.log("Result of getElementByTagName:");
for(var i=0;i<getElementByTagName.length;i++){
console.log(getElementByTagName[i]);
}
console.log("Result of getElementByName : ");
for(var i=0;i<getElementByName.length;i++){
console.log(getElementByName[i]);
}
MedTech
Elements Handling: querySelector
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 10
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title> My Test Page </title>
</head>
<body>
<div id="myMenu">
<div class="item">
<span> Menu 1</span>
<span> Menu 2</span>
</div>
<div class="Pub">
<span> Pub 1</span>
<span> Pub 2</span>
</div>
</div>
<div id="content">
<span>
Here is my content
</span>
</div>
A weird form to test
getElementsByName() :
<form name="myName">
<input type="password"
name= "myName">
<input>
<button name="myName"></button>
</form>
<script src="test.js"></script>
</body>
</html>
var querySel=
document.querySelector("head title");
var querySelAll=
document.querySelectorAll("#myMenu .item
span");
console.log("Result of querySelector : " +
querySel.textContent);
console.log("Result of querySelectorAll : ");
for(var i=0;i<querySelAll.length;i++){
console.log(querySelAll[i].textContent);
}
MedTech
Elements Handling: Attributes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 11
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>
My Test Page
</title>
</head>
<body>
You will find my CV
<a id= "link"
href="www.myCv.tn">here</a>.
<script
src="test2.js"></script>
</body>
</html>
var link=document.getElementById("myLink");
var dest=link.getAttribute("href");
console.log("URL of the link before
modification : "+ dest);
var newCVLink = prompt();
link.setAttribute("href", newCVLink);
dest = link.getAttribute("href");
console.log("URL of the link after
modification : "+dest);
• To read, create or update an attribute of an element of the DOM, the
object Element defines two methods:
• getAttribute(): gets the value of an attribute
• setAttribute(): updates the value of an attribute
MedTech
Elements Handling: Attributes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 12
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>
My Test Page
</title>
</head>
<body>
You will find my CV
<a id= "myLink"
href="www.myCv.tn">here</a>.
<script
src="test2.js"></script>
</body>
</html>
var anotherLink=
document.getElementById("myLink");
alert("old link: "+ anotherLink.href);
anotherLink.href="newLien.tn"
alert("new link: "+ anotherLink.href);
• Another way of accessing an attribute is to use .attrName
• Not supported by all the browsers
MedTech
Elements Handling: innerHTML
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 13
Client-Side JavaScript
<html>
<head>
<meta charset="utf-8" />
<title> My Test Page</title>
</head>
<body>
<div id="div">
<p> I am in a div with id
div</p>
</div>
<div id="div1">
<p> I am in a div with id
div 1</p>
</div>
<script src="test3.js"></script>
</body>
</html>
var myDiv=document.getElementById("div");
var myDiv1=document.getElementById("div1");
function innerTest(){
myDiv1.textContent="I delete everything in my
road <b>i'm a monster 3:)</b> ";
myDiv.innerHTML="I delete everything in my road
<b>i'm a monster too 3:)</b> ";
}
• innerHTML
• Gets the child HTML code of a tag
• Adds directly HTML content
• textContent (or innerText, this latter not supported in all browsers)
• Gets only the text, without the HTML tags
MedTech
Elements Handling: querySelector
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 14
Client-Side JavaScript
• querySelector() returns the first element in the document that
matches a specified CSS selector(s) in the document.
• To return all the matches, use the querySelectorAll() method instead.
function querySelTest() {
var elem = document.querySelector("p");
console.log(elem);
var elems = document.querySelectorAll("p");
console.log(elems);
}
MedTech
Elements Handling: Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 15
Client-Side JavaScript
• There are some
useful attributes
that help extracting
DOM elements
• nodeType: gets the
type of DOM node, in
the form of a
number such as:
• 1: Element
• 2: Attribute
• 3: Text
• nodeName: gets the
name of the element
node in Uppercase
Node Type Name of the constant Value
Element Node.ELEMENT_NODE 1
Attribute Node.ATTRIBUTE_NODE 2
Text Node Node.TEXT_NODE 3
Node.CDATA_SECTION_NODE 4
Node.ENTITY_REFERENCE_NODE 5
Node.ENTITYT_NODE 6
Instruction Node.PROCESSING_INSTRUCTION_NODE 7
Comment Node.COMMENT_NODE 8
Node.DOCUMENT_NODE 9
Node.TYPE_NODE 10
XML Fragment Node.DOCUMENT_FRAGMENT_NODE 11
Node.NOTATION_NODE 12
MedTech
Elements Handling: Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 16
Client-Side JavaScript
• Other attributes to go through the DOM
• parentNode: enables to access the parent node of an element
• firstChild : enables to access the first child of a node
• lastChild: enables to access the last child of a node
• firstElementChild : enables to access the first element child of a node
• lastElementChild : enables to access the last element child of a node
• childNodes: returns an array containing all the child nodes
• nextSibling: accesses the next node, sibling of the current
• previousSibling: accesses the previous node
MedTech
Elements Handling: Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 17
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title> My Test Page </title>
</head>
<body>
<p id="para"> You will find my CV
<a id="link"
href="www.myCv.tn">here</a>. Please
call me on <i>222222</i> for
further details</p>
<div id="myMenu">
<div id="item">
<span>menu 1</span>
<span>menu 2</span>
</div>
<div class="pub">
<span>Pub 1</span>
<span>Pub 2</span>
</div>
</div>
<script src="test4.js">
</script>
</body>
</html>
var nod = document.getElementById("para");
console.log("Hi I am the node "+nod.nodeName) ;
console.log("Difference between firstChild and
firstElementChild ");
console.log("Hi, I am the first child : "
+nod.firstChild);
console.log("Hi, I am the first Elementchild : "
+nod.firstElementChild);
console.log(" Next Sibling: "+nod.nextSibling);
console.log(" Next Element Sibling: "
+nod.nextElementSibling);
MedTech
Elements Handling: Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 18
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title> My Test Page </title>
</head>
<body>
<p id="para"> You will find my CV
<a id="link"
href="www.myCv.tn">here</a>. Please
call me on <i>222222</i> for
further details</p>
<div id="myMenu">
<div id="item">
<span>menu 1</span>
<span>menu 2</span>
</div>
<div class="pub">
<span>Pub 1</span>
<span>Pub 2</span>
</div>
</div>
<script src="test5.js">
</script>
</body>
</html>
var nod=document.getElementById("para");
console.log("Hi, I am the node: "+nod.nodeName) ;
console.log("These are my children : ");
var children=nod.childNodes;
for (var i=0;i<children.length;i++){
console.log("Hi, I am child number:"+ (i+1)+"
my name is: "
+children[i].nodeName+" and my content text
is:");
if(children[i].nodeType==Node.ELEMENT_NODE){
console.log(children[i].firstChild.data);
}else{
console.log(children[i].data);
}
}
MedTech
Elements Handling: Adding Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 19
Client-Side JavaScript
• In order to add a new node in the DOM, follow the next steps:
• Create the element to add, using the method createElement() that takes a
String as a parameter, representing the name of the element to create.
• Add the necessary attributes to the element using one of the following
methods:
• setAttribute(attr_name, value)
• <obj>.<attribute> = value
• Insert the element in the DOM using the method appendChild() that takes
as a parameter the element to insert, and adds this element as the last
child of the object calling the method.
• To add a text node, use the method createTextNode() with the
corresponding text as a parameter
• The method insertBefore(n , s) inserts the node n before the node s
MedTech
Elements Handling: Adding Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 20
Client-Side JavaScript
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title> My Test Page </title>
</head>
<body>
<p id="para"> You will find my CV
<a id="link"
href="www.myCv.tn">here</a>. </p>
<div id="myImage">
<button onclick="addImage()">
Add Image
</button>
</div>
<div id="myMenu">
<div id="item">
<span>menu 1</span>
<span>menu 2</span>
</div>
<div class="pub">
<span>Pub 1</span>
<span>Pub 2</span>
</div>
</div>
<script src="test6.js">
</script>
</body>
</html>
var nod = document.getElementById("myImage");
function addImage() {
var newNode = document.createElement("img");
newNode.id = "jsimg";
newNode.src = "js.jpg";
newNode.alt = "Javascript…";
newNode.width = "200";
newNode.height = "200";
nod.appendChild(newNode);
}
MedTech
Elements Handling: Cloning Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 21
Client-Side JavaScript
• In order to clone a node in the DOM, call the method cloneNode()
• It takes a boolean as a parameter, which value is:
• True: if the node will be cloned with its children and attributes
• False: if the node will be cloned without them
function clone() {
var trueClonedNode = nod.cloneNode(true);
var falseClonedNode = nod.cloneNode(false);
cloneDiv.appendChild(trueClonedNode);
cloneDiv.appendChild(falseClonedNode);
}
MedTech
Elements Handling: Deleting Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 22
Client-Side JavaScript
• In order to delete a node from the DOM, call the method removeChild()
from the father node
• This method takes as a parameter the node to delete
• The return value is a reference of the deleted node
function deleteMe() {
var elem = document.getElementById("item");
elem.removeChild(elem.firstElementChild);
}
MedTech
Elements Handling: Replacing Nodes
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 23
Client-Side JavaScript
• In order to replace a node in the DOM, call replaceChild() from the
father node.
• This method takes as a parameter the new node, followed by the old node
function replaceMe() {
var nod = document.getElementById("item");
var newnod = document.
getElementsByClassName("pub")[0].
cloneNode(true);
nod.replaceChild(newnod, nod.lastElementChild);
}
MedTech
Events Handling
• HTML DOM events allow JavaScript to register different event handlers
on elements in an HTML document.
• Events are normally used in combination with functions, and the
function will not be executed before the event occurs (such as when a
user clicks a button).
• Every DOM element has its own addEventListener method, which allows
you to listen specifically on that element.
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 24
Client-Side JavaScript
<button>Click me</button>
<p>No handler here.</p>
<script>
var button = document.querySelector("button");
button.addEventListener("click", function() {
alert("Button clicked.");
});
</script>
MedTech
Events Handling
• The removeEventListener method, called with arguments similar to
as addEventListener, removes a handler.
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 25
Client-Side JavaScript
<button>Act-once button</button>
<script>
var button = document.querySelector("button");
function once() {
console.log("Done.");
button.removeEventListener("click", once);
}
button.addEventListener("click", once);
</script>
MedTech
Events Handling: Event Objects
• The event object, passed as a parameter to addEventListener, gives
additional information about the event
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 26
Client-Side JavaScript
<button>Click me any way you want</button>
<script>
var button = document.querySelector("button");
button.addEventListener("mousedown", function(event) {
if (event.which == 1)
console.log("Left button");
else if (event.which == 2)
console.log("Middle button");
else if (event.which == 3)
console.log("Right button");
});
</script>
MedTech
Events Handling: Event Propagation
• DOM elements can be nested inside each other. And somehow, the
handler of the parent works even if you click on it’s child.
• If a button inside a paragraph is clicked, event handlers on the paragraph
will also receive the click event.
• Event Propagation:
• In all browsers, except IE <9, there are two stages of event propagation:
• The event first goes down, from the topmost element to the innermost element:
called Capturing (1->2->3)
• It then bubbles up, to trigger on parents in nesting order: called Bubbling
(3->2->1)
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 27
Client-Side JavaScript
11
3
2
MedTech
Events Handling: Event Propagation
• All methods of event handling ignore the capturing phase.
• By default, only the bubbling takes place
• Using addEventListener with last argument true is only the way to catch the
event at capturing.
• The event object has a target property refering to the node where they
originated
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 28
Client-Side JavaScript
<div class="d1"> 1
<div class="d1"> 2
<div class="d1"> 3 </div>
</div> </div>
<script>
var divs= document. getElementsByTagName("div");
for (var i = 0; i < divs.length; i++) {
divs[i].addEventListener('click', function(event) {
alert("target: " + event.target.className + " - this: " + this.className)
});
}
</script
MedTech
Events Handling: Event Propagation
• You can stop the event propagation by running
event.stopPropagation()
• Or event.cancelBubble = true for IE <9
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 29
Client-Side JavaScript
<p>A paragraph with a <button>button</button>.</p>
<script>
var para = document.querySelector("p");
var button = document.querySelector("button");
para.addEventListener("mousedown", function() {
console.log("Handler for paragraph.");
});
button.addEventListener("mousedown", function(event) {
console.log("Handler for button.");
if (event.which == 3) event.stopPropagation();
});
</script>
MedTech
Events Handling: Events
• Javascript defines various DOM events, such as:
• onmouseover: when the mouse hovers over the element
• onblur: when the element loses focus
• onfocus: when the element gains focus
• onselect: when the element is selected
• onchange: when the content of the element is changed
• onsubmit: when a form is submitted
• onload: when an element ends its loading phase
• …
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 30
Client-Side JavaScript
MedTech
RUNNING EXAMPLE
JavaScript/ECMAScript
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 31
MedTech
Todo App
• Go back to the TODO app you created the last time: we are going to add
an interface to it!
• Follow these steps:
• Step1: Interface
• Create an interface that looks like the following
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 32
Running Example
MedTech
Todo App
• For now, all the display will be on the console
• Step 2:
• Add a listener to the button add, that adds the written element to the
todolist
• Add a listener for the modify button, that modifies the element which id is
given in the first input with the task in the second
• Add a listener for the delete button, that deletes the task which id is given
in the input
• Add a listener to the toggle completed button, that does that to the task
which id is given in the input
• Add a listener to the ToggleAll button
• Step 3:
• Now, the tasks will be displayed in an unordered list under the form. Every
modification of the list is immediately impacted on the display
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 33
Running Example
MedTech
References
Dr. Lilia SFAXI
www.liliasfaxi.wix.com/liliasfaxi
Slide 34
• M. Haverbeke, Eloquent JavaScript: A modern introduction to
programming, 2014
• Textbook
• D. Flanagan, JavaScript: The Definitive Guide, 6th edition, O’reilly, 2011

More Related Content

What's hot

Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
Cyber Security Alliance
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
Nitin Pai
 

What's hot (20)

Node JS Crash Course
Node JS Crash CourseNode JS Crash Course
Node JS Crash Course
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
jQuery
jQueryjQuery
jQuery
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Dom
Dom Dom
Dom
 
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
Asfws 2014 slides why .net needs ma-cs and other serial(-ization) tales_v2.0
 
Node js Modules and Event Emitters
Node js Modules and Event EmittersNode js Modules and Event Emitters
Node js Modules and Event Emitters
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Bootstrap PPT Part - 2
Bootstrap PPT Part - 2Bootstrap PPT Part - 2
Bootstrap PPT Part - 2
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
BlueHat v17 || Dangerous Contents - Securing .Net Deserialization
BlueHat v17 || Dangerous Contents - Securing .Net Deserialization BlueHat v17 || Dangerous Contents - Securing .Net Deserialization
BlueHat v17 || Dangerous Contents - Securing .Net Deserialization
 
jQuery
jQueryjQuery
jQuery
 
Express node js
Express node jsExpress node js
Express node js
 

Viewers also liked

Viewers also liked (17)

Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Introduction au Web
Introduction au WebIntroduction au Web
Introduction au Web
 
Core JavaScript
Core JavaScriptCore JavaScript
Core JavaScript
 
Angular
AngularAngular
Angular
 
Mobile developement
Mobile developementMobile developement
Mobile developement
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJS
 
BigData_TP4 : Cassandra
BigData_TP4 : CassandraBigData_TP4 : Cassandra
BigData_TP4 : Cassandra
 
BigData_TP5 : Neo4J
BigData_TP5 : Neo4JBigData_TP5 : Neo4J
BigData_TP5 : Neo4J
 
BigData_TP2: Design Patterns dans Hadoop
BigData_TP2: Design Patterns dans HadoopBigData_TP2: Design Patterns dans Hadoop
BigData_TP2: Design Patterns dans Hadoop
 
BigData_TP1: Initiation à Hadoop et Map-Reduce
BigData_TP1: Initiation à Hadoop et Map-ReduceBigData_TP1: Initiation à Hadoop et Map-Reduce
BigData_TP1: Initiation à Hadoop et Map-Reduce
 
BigData_TP3 : Spark
BigData_TP3 : SparkBigData_TP3 : Spark
BigData_TP3 : Spark
 
BigData_Chp5: Putting it all together
BigData_Chp5: Putting it all togetherBigData_Chp5: Putting it all together
BigData_Chp5: Putting it all together
 
BigData_Chp4: NOSQL
BigData_Chp4: NOSQLBigData_Chp4: NOSQL
BigData_Chp4: NOSQL
 
BigData_Chp3: Data Processing
BigData_Chp3: Data ProcessingBigData_Chp3: Data Processing
BigData_Chp3: Data Processing
 
BigData_Chp2: Hadoop & Map-Reduce
BigData_Chp2: Hadoop & Map-ReduceBigData_Chp2: Hadoop & Map-Reduce
BigData_Chp2: Hadoop & Map-Reduce
 
BigData_Chp1: Introduction à la Big Data
BigData_Chp1: Introduction à la Big DataBigData_Chp1: Introduction à la Big Data
BigData_Chp1: Introduction à la Big Data
 

Similar to Client-side JavaScript

01 Introduction - JavaScript Development
01 Introduction - JavaScript Development01 Introduction - JavaScript Development
01 Introduction - JavaScript Development
Tommy Vercety
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
Laila Buncab
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
Terry Yoast
 
JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & event
Borey Lim
 

Similar to Client-side JavaScript (20)

JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
J query
J queryJ query
J query
 
Dom
DomDom
Dom
 
The Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has ArrivedThe Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has Arrived
 
Javascript 2
Javascript 2Javascript 2
Javascript 2
 
Web components
Web componentsWeb components
Web components
 
01 Introduction - JavaScript Development
01 Introduction - JavaScript Development01 Introduction - JavaScript Development
01 Introduction - JavaScript Development
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 
Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
Jquery library
Jquery libraryJquery library
Jquery library
 
jQuery
jQueryjQuery
jQuery
 
JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & event
 
Web component driven development
Web component driven developmentWeb component driven development
Web component driven development
 
Dom
DomDom
Dom
 
20131108 cs query by howard
20131108 cs query by howard20131108 cs query by howard
20131108 cs query by howard
 
2.java script dom
2.java script  dom2.java script  dom
2.java script dom
 

More from Lilia Sfaxi

More from Lilia Sfaxi (20)

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdf
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdf
 
Lab3-DB_Neo4j
Lab3-DB_Neo4jLab3-DB_Neo4j
Lab3-DB_Neo4j
 
Lab2-DB-Mongodb
Lab2-DB-MongodbLab2-DB-Mongodb
Lab2-DB-Mongodb
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-Correction
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-Correction
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-Correction
 
TD4-UML
TD4-UMLTD4-UML
TD4-UML
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-Correction
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-Séquences
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-Correction
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - Correction
 
TD1 - UML - DCU
TD1 - UML - DCUTD1 - UML - DCU
TD1 - UML - DCU
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correction
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrage
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intents
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web services
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancés
 

Recently uploaded

Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...
Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...
Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...
Klinik kandungan
 
一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样
一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样
一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样
dyuozua
 
一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样
一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样
一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样
dyuozua
 
0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR
0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR
0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR
jualobat34
 
一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样
一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样
一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样
dyuozua
 
一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样
一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样
一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样
dyuozua
 
Corporate Presentation Probe May 2024.pdf
Corporate Presentation Probe May 2024.pdfCorporate Presentation Probe May 2024.pdf
Corporate Presentation Probe May 2024.pdf
Probe Gold
 
皇后大学毕业证
皇后大学毕业证皇后大学毕业证
皇后大学毕业证
dyuozua
 
一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样
一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样
一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样
dyuozua
 
一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样
一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样
一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样
dyuozua
 
Terna - 1Q 2024 Consolidated Results Presentation
Terna - 1Q 2024 Consolidated Results PresentationTerna - 1Q 2024 Consolidated Results Presentation
Terna - 1Q 2024 Consolidated Results Presentation
Terna SpA
 
一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样
一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样
一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样
dyuozua
 

Recently uploaded (20)

Camil Institutional Presentation_Mai24.pdf
Camil Institutional Presentation_Mai24.pdfCamil Institutional Presentation_Mai24.pdf
Camil Institutional Presentation_Mai24.pdf
 
abortion pills in Riyadh+966572737505 Cytotec Riyadh
abortion pills in  Riyadh+966572737505    Cytotec Riyadhabortion pills in  Riyadh+966572737505    Cytotec Riyadh
abortion pills in Riyadh+966572737505 Cytotec Riyadh
 
Western Copper and Gold - May 2024 Presentation
Western Copper and Gold - May 2024 PresentationWestern Copper and Gold - May 2024 Presentation
Western Copper and Gold - May 2024 Presentation
 
Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...
Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...
Jual obat aborsi Tawangmangu ( 085657271886 ) Cytote pil telat bulan penggugu...
 
Collective Mining | Corporate Presentation - May 2024
Collective Mining | Corporate Presentation - May 2024Collective Mining | Corporate Presentation - May 2024
Collective Mining | Corporate Presentation - May 2024
 
Teekay Corporation Q1-24 Earnings Results
Teekay Corporation Q1-24 Earnings ResultsTeekay Corporation Q1-24 Earnings Results
Teekay Corporation Q1-24 Earnings Results
 
一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样
一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样
一比一原版(UofT毕业证书)多伦多大学毕业证成绩单原件一模一样
 
一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样
一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样
一比一原版(Mac毕业证书)麦克马斯特大学毕业证成绩单原件一模一样
 
0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR
0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR
0838*4800*7379 JUAL OBAT ABORSI CYTOTEC BOGOR
 
一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样
一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样
一比一原版(UNITEC毕业证书)UNITEC理工学院毕业证成绩单原件一模一样
 
一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样
一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样
一比一原版(Otago毕业证书)奥塔哥大学毕业证成绩单原件一模一样
 
Corporate Presentation Probe May 2024.pdf
Corporate Presentation Probe May 2024.pdfCorporate Presentation Probe May 2024.pdf
Corporate Presentation Probe May 2024.pdf
 
皇后大学毕业证
皇后大学毕业证皇后大学毕业证
皇后大学毕业证
 
一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样
一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样
一比一原版(UofG毕业证书)圭尔夫大学毕业证成绩单原件一模一样
 
一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样
一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样
一比一原版(ArtEZ毕业证书)ArtEZ艺术学院毕业证成绩单原件一模一样
 
Terna - 1Q 2024 Consolidated Results Presentation
Terna - 1Q 2024 Consolidated Results PresentationTerna - 1Q 2024 Consolidated Results Presentation
Terna - 1Q 2024 Consolidated Results Presentation
 
Osisko Development - Investor Presentation - May 2024
Osisko Development - Investor Presentation - May 2024Osisko Development - Investor Presentation - May 2024
Osisko Development - Investor Presentation - May 2024
 
Teck Investor Presentation, April 24, 2024
Teck Investor Presentation, April 24, 2024Teck Investor Presentation, April 24, 2024
Teck Investor Presentation, April 24, 2024
 
Teck Sustainability Leadership, April 26, 2024
Teck Sustainability Leadership, April 26, 2024Teck Sustainability Leadership, April 26, 2024
Teck Sustainability Leadership, April 26, 2024
 
一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样
一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样
一比一原版(RUG毕业证书)格罗宁根大学毕业证成绩单原件一模一样
 

Client-side JavaScript

  • 1. MedTech Chp3- Client-Side JavaScript DOM, Forms and HTML Manipulation Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 1 MedTech – Mediterranean Institute of Technology CS-Web and Mobile Development MedTech
  • 2. MedTech Window object • Main entry point to all client-side JS features and APIs • Represents a web browser window or frame • Referred to with the identifier window • It is the global object, and can be omitted • Defines properties like: • location (URL currently displayed) • Ex: window.location = ”http://medtech.tn/” navigates to the website of the institute • alert() : displays a message in a dialog box • setTimeout(): registers a function to be invoked after a specific amount of time • document: represents the content displayed in the window Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 2 Client-Side JavaScript
  • 3. MedTech DOM: Document Object Model • W3C Standard • Object-oriented model for representing HTML and XML documents • The HTML DOM is a standard object model and programming interface for HTML. It defines: • The HTML elements as objects • The properties of all HTML elements • The methods to access all HTML elements • The events for all HTML elements • The HTML DOM is a standard for how to get, change, add, or delete HTML elements. Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 3 Client-Side JavaScript
  • 4. DOM: Document Object Model Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 4 Client-Side JavaScript <html> <head> <title> My title</title> </head> <body> <h1> A heading </h1> <a href="link text"> and a link </a> </body> </html>
  • 5. MedTech GOING THROUGH THE DOM ELEMENTS Client-Side JavaScript Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 5
  • 6. MedTech DOM Programming Interface • The HTML DOM can be accessed with JavaScript (and with other programming languages). • In the DOM, all HTML elements are defined as objects. • The DOM Programming Interface is the set of properties and methods of each object • A property is a value that you can get or set (like changing the content of an HTML element). • A method is an action you can do (like add or deleting an HTML element). • The top of the DOM tree is the object window • The object document is a child element of window • It represents the web page and enables to access the HTML element Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 6 Client-Side JavaScript
  • 7. MedTech DOM Types • Node: The most generic type, all DOM elements are nodes • Element: HTML or XML Element • HTML Element: • An Element in an HTML DOM is an HTML Element • Represents the HTML elements of the document: the tags • All DOM elements are objects, thus having their own methods and attributes • Some methods and attributes are common to all DOM objects, as they are all of the same type : Node Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 7 Client-Side JavaScript Node Element HTMLElement HTMLDivElement
  • 8. MedTech Elements Handling • To access HTML elements using the DOM, several methods are offered by the object document • getElementById(): accesses an element via its id • getElementByTagName(): gets all the elements of the given tag as an array of objects • getElementsByName(): returns an array of objects containing all the elements whose attribute name is equal to the given argument • querySelector() • This function takes as a parameter a string representing a CSS selector • Returns the first element that corresponds to this selector • querySelectorAll() • Returns an array of all the elements corresponding to the given selector Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 8 Client-Side JavaScript
  • 9. MedTech Elements Handling: getElementBy… Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 9 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> <div id="myMenu"> <div class="item"> <span> Menu 1</span> <span> Menu 2</span> </div> <div class="Pub"> <span> Pub 1</span> <span> Pub 2</span> </div> </div> <div id="content"> <span> Here is my content </span> </div> A weird form to test getElementsByName() : <form name="myName"> <input type="password" name= "myName"> <input> <button name="myName"></button> </form> <script src="test.js"></script> </body> </html> var getByid= document.getElementById('myMenu'); var getElementByTagName = document.getElementsByTagName("div"); var getElementByName = document.getElementsByName("myName"); console.log("Result of getElementById : " + getByid.textContent); console.log("Result of getElementByTagName:"); for(var i=0;i<getElementByTagName.length;i++){ console.log(getElementByTagName[i]); } console.log("Result of getElementByName : "); for(var i=0;i<getElementByName.length;i++){ console.log(getElementByName[i]); }
  • 10. MedTech Elements Handling: querySelector Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 10 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> <div id="myMenu"> <div class="item"> <span> Menu 1</span> <span> Menu 2</span> </div> <div class="Pub"> <span> Pub 1</span> <span> Pub 2</span> </div> </div> <div id="content"> <span> Here is my content </span> </div> A weird form to test getElementsByName() : <form name="myName"> <input type="password" name= "myName"> <input> <button name="myName"></button> </form> <script src="test.js"></script> </body> </html> var querySel= document.querySelector("head title"); var querySelAll= document.querySelectorAll("#myMenu .item span"); console.log("Result of querySelector : " + querySel.textContent); console.log("Result of querySelectorAll : "); for(var i=0;i<querySelAll.length;i++){ console.log(querySelAll[i].textContent); }
  • 11. MedTech Elements Handling: Attributes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 11 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> You will find my CV <a id= "link" href="www.myCv.tn">here</a>. <script src="test2.js"></script> </body> </html> var link=document.getElementById("myLink"); var dest=link.getAttribute("href"); console.log("URL of the link before modification : "+ dest); var newCVLink = prompt(); link.setAttribute("href", newCVLink); dest = link.getAttribute("href"); console.log("URL of the link after modification : "+dest); • To read, create or update an attribute of an element of the DOM, the object Element defines two methods: • getAttribute(): gets the value of an attribute • setAttribute(): updates the value of an attribute
  • 12. MedTech Elements Handling: Attributes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 12 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> You will find my CV <a id= "myLink" href="www.myCv.tn">here</a>. <script src="test2.js"></script> </body> </html> var anotherLink= document.getElementById("myLink"); alert("old link: "+ anotherLink.href); anotherLink.href="newLien.tn" alert("new link: "+ anotherLink.href); • Another way of accessing an attribute is to use .attrName • Not supported by all the browsers
  • 13. MedTech Elements Handling: innerHTML Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 13 Client-Side JavaScript <html> <head> <meta charset="utf-8" /> <title> My Test Page</title> </head> <body> <div id="div"> <p> I am in a div with id div</p> </div> <div id="div1"> <p> I am in a div with id div 1</p> </div> <script src="test3.js"></script> </body> </html> var myDiv=document.getElementById("div"); var myDiv1=document.getElementById("div1"); function innerTest(){ myDiv1.textContent="I delete everything in my road <b>i'm a monster 3:)</b> "; myDiv.innerHTML="I delete everything in my road <b>i'm a monster too 3:)</b> "; } • innerHTML • Gets the child HTML code of a tag • Adds directly HTML content • textContent (or innerText, this latter not supported in all browsers) • Gets only the text, without the HTML tags
  • 14. MedTech Elements Handling: querySelector Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 14 Client-Side JavaScript • querySelector() returns the first element in the document that matches a specified CSS selector(s) in the document. • To return all the matches, use the querySelectorAll() method instead. function querySelTest() { var elem = document.querySelector("p"); console.log(elem); var elems = document.querySelectorAll("p"); console.log(elems); }
  • 15. MedTech Elements Handling: Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 15 Client-Side JavaScript • There are some useful attributes that help extracting DOM elements • nodeType: gets the type of DOM node, in the form of a number such as: • 1: Element • 2: Attribute • 3: Text • nodeName: gets the name of the element node in Uppercase Node Type Name of the constant Value Element Node.ELEMENT_NODE 1 Attribute Node.ATTRIBUTE_NODE 2 Text Node Node.TEXT_NODE 3 Node.CDATA_SECTION_NODE 4 Node.ENTITY_REFERENCE_NODE 5 Node.ENTITYT_NODE 6 Instruction Node.PROCESSING_INSTRUCTION_NODE 7 Comment Node.COMMENT_NODE 8 Node.DOCUMENT_NODE 9 Node.TYPE_NODE 10 XML Fragment Node.DOCUMENT_FRAGMENT_NODE 11 Node.NOTATION_NODE 12
  • 16. MedTech Elements Handling: Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 16 Client-Side JavaScript • Other attributes to go through the DOM • parentNode: enables to access the parent node of an element • firstChild : enables to access the first child of a node • lastChild: enables to access the last child of a node • firstElementChild : enables to access the first element child of a node • lastElementChild : enables to access the last element child of a node • childNodes: returns an array containing all the child nodes • nextSibling: accesses the next node, sibling of the current • previousSibling: accesses the previous node
  • 17. MedTech Elements Handling: Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 17 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> <p id="para"> You will find my CV <a id="link" href="www.myCv.tn">here</a>. Please call me on <i>222222</i> for further details</p> <div id="myMenu"> <div id="item"> <span>menu 1</span> <span>menu 2</span> </div> <div class="pub"> <span>Pub 1</span> <span>Pub 2</span> </div> </div> <script src="test4.js"> </script> </body> </html> var nod = document.getElementById("para"); console.log("Hi I am the node "+nod.nodeName) ; console.log("Difference between firstChild and firstElementChild "); console.log("Hi, I am the first child : " +nod.firstChild); console.log("Hi, I am the first Elementchild : " +nod.firstElementChild); console.log(" Next Sibling: "+nod.nextSibling); console.log(" Next Element Sibling: " +nod.nextElementSibling);
  • 18. MedTech Elements Handling: Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 18 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> <p id="para"> You will find my CV <a id="link" href="www.myCv.tn">here</a>. Please call me on <i>222222</i> for further details</p> <div id="myMenu"> <div id="item"> <span>menu 1</span> <span>menu 2</span> </div> <div class="pub"> <span>Pub 1</span> <span>Pub 2</span> </div> </div> <script src="test5.js"> </script> </body> </html> var nod=document.getElementById("para"); console.log("Hi, I am the node: "+nod.nodeName) ; console.log("These are my children : "); var children=nod.childNodes; for (var i=0;i<children.length;i++){ console.log("Hi, I am child number:"+ (i+1)+" my name is: " +children[i].nodeName+" and my content text is:"); if(children[i].nodeType==Node.ELEMENT_NODE){ console.log(children[i].firstChild.data); }else{ console.log(children[i].data); } }
  • 19. MedTech Elements Handling: Adding Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 19 Client-Side JavaScript • In order to add a new node in the DOM, follow the next steps: • Create the element to add, using the method createElement() that takes a String as a parameter, representing the name of the element to create. • Add the necessary attributes to the element using one of the following methods: • setAttribute(attr_name, value) • <obj>.<attribute> = value • Insert the element in the DOM using the method appendChild() that takes as a parameter the element to insert, and adds this element as the last child of the object calling the method. • To add a text node, use the method createTextNode() with the corresponding text as a parameter • The method insertBefore(n , s) inserts the node n before the node s
  • 20. MedTech Elements Handling: Adding Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 20 Client-Side JavaScript <!doctype html> <html> <head> <meta charset="utf-8" /> <title> My Test Page </title> </head> <body> <p id="para"> You will find my CV <a id="link" href="www.myCv.tn">here</a>. </p> <div id="myImage"> <button onclick="addImage()"> Add Image </button> </div> <div id="myMenu"> <div id="item"> <span>menu 1</span> <span>menu 2</span> </div> <div class="pub"> <span>Pub 1</span> <span>Pub 2</span> </div> </div> <script src="test6.js"> </script> </body> </html> var nod = document.getElementById("myImage"); function addImage() { var newNode = document.createElement("img"); newNode.id = "jsimg"; newNode.src = "js.jpg"; newNode.alt = "Javascript…"; newNode.width = "200"; newNode.height = "200"; nod.appendChild(newNode); }
  • 21. MedTech Elements Handling: Cloning Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 21 Client-Side JavaScript • In order to clone a node in the DOM, call the method cloneNode() • It takes a boolean as a parameter, which value is: • True: if the node will be cloned with its children and attributes • False: if the node will be cloned without them function clone() { var trueClonedNode = nod.cloneNode(true); var falseClonedNode = nod.cloneNode(false); cloneDiv.appendChild(trueClonedNode); cloneDiv.appendChild(falseClonedNode); }
  • 22. MedTech Elements Handling: Deleting Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 22 Client-Side JavaScript • In order to delete a node from the DOM, call the method removeChild() from the father node • This method takes as a parameter the node to delete • The return value is a reference of the deleted node function deleteMe() { var elem = document.getElementById("item"); elem.removeChild(elem.firstElementChild); }
  • 23. MedTech Elements Handling: Replacing Nodes Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 23 Client-Side JavaScript • In order to replace a node in the DOM, call replaceChild() from the father node. • This method takes as a parameter the new node, followed by the old node function replaceMe() { var nod = document.getElementById("item"); var newnod = document. getElementsByClassName("pub")[0]. cloneNode(true); nod.replaceChild(newnod, nod.lastElementChild); }
  • 24. MedTech Events Handling • HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. • Events are normally used in combination with functions, and the function will not be executed before the event occurs (such as when a user clicks a button). • Every DOM element has its own addEventListener method, which allows you to listen specifically on that element. Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 24 Client-Side JavaScript <button>Click me</button> <p>No handler here.</p> <script> var button = document.querySelector("button"); button.addEventListener("click", function() { alert("Button clicked."); }); </script>
  • 25. MedTech Events Handling • The removeEventListener method, called with arguments similar to as addEventListener, removes a handler. Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 25 Client-Side JavaScript <button>Act-once button</button> <script> var button = document.querySelector("button"); function once() { console.log("Done."); button.removeEventListener("click", once); } button.addEventListener("click", once); </script>
  • 26. MedTech Events Handling: Event Objects • The event object, passed as a parameter to addEventListener, gives additional information about the event Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 26 Client-Side JavaScript <button>Click me any way you want</button> <script> var button = document.querySelector("button"); button.addEventListener("mousedown", function(event) { if (event.which == 1) console.log("Left button"); else if (event.which == 2) console.log("Middle button"); else if (event.which == 3) console.log("Right button"); }); </script>
  • 27. MedTech Events Handling: Event Propagation • DOM elements can be nested inside each other. And somehow, the handler of the parent works even if you click on it’s child. • If a button inside a paragraph is clicked, event handlers on the paragraph will also receive the click event. • Event Propagation: • In all browsers, except IE <9, there are two stages of event propagation: • The event first goes down, from the topmost element to the innermost element: called Capturing (1->2->3) • It then bubbles up, to trigger on parents in nesting order: called Bubbling (3->2->1) Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 27 Client-Side JavaScript 11 3 2
  • 28. MedTech Events Handling: Event Propagation • All methods of event handling ignore the capturing phase. • By default, only the bubbling takes place • Using addEventListener with last argument true is only the way to catch the event at capturing. • The event object has a target property refering to the node where they originated Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 28 Client-Side JavaScript <div class="d1"> 1 <div class="d1"> 2 <div class="d1"> 3 </div> </div> </div> <script> var divs= document. getElementsByTagName("div"); for (var i = 0; i < divs.length; i++) { divs[i].addEventListener('click', function(event) { alert("target: " + event.target.className + " - this: " + this.className) }); } </script
  • 29. MedTech Events Handling: Event Propagation • You can stop the event propagation by running event.stopPropagation() • Or event.cancelBubble = true for IE <9 Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 29 Client-Side JavaScript <p>A paragraph with a <button>button</button>.</p> <script> var para = document.querySelector("p"); var button = document.querySelector("button"); para.addEventListener("mousedown", function() { console.log("Handler for paragraph."); }); button.addEventListener("mousedown", function(event) { console.log("Handler for button."); if (event.which == 3) event.stopPropagation(); }); </script>
  • 30. MedTech Events Handling: Events • Javascript defines various DOM events, such as: • onmouseover: when the mouse hovers over the element • onblur: when the element loses focus • onfocus: when the element gains focus • onselect: when the element is selected • onchange: when the content of the element is changed • onsubmit: when a form is submitted • onload: when an element ends its loading phase • … Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 30 Client-Side JavaScript
  • 31. MedTech RUNNING EXAMPLE JavaScript/ECMAScript Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 31
  • 32. MedTech Todo App • Go back to the TODO app you created the last time: we are going to add an interface to it! • Follow these steps: • Step1: Interface • Create an interface that looks like the following Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 32 Running Example
  • 33. MedTech Todo App • For now, all the display will be on the console • Step 2: • Add a listener to the button add, that adds the written element to the todolist • Add a listener for the modify button, that modifies the element which id is given in the first input with the task in the second • Add a listener for the delete button, that deletes the task which id is given in the input • Add a listener to the toggle completed button, that does that to the task which id is given in the input • Add a listener to the ToggleAll button • Step 3: • Now, the tasks will be displayed in an unordered list under the form. Every modification of the list is immediately impacted on the display Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 33 Running Example
  • 34. MedTech References Dr. Lilia SFAXI www.liliasfaxi.wix.com/liliasfaxi Slide 34 • M. Haverbeke, Eloquent JavaScript: A modern introduction to programming, 2014 • Textbook • D. Flanagan, JavaScript: The Definitive Guide, 6th edition, O’reilly, 2011