Java Script
Java Script
class Person {
constructor(name)
{ this.name = name; }
getName() { return this.name; }
}
● Method ● Description
● setDate() ● Set the day as a
number (1-31)
● setFullYear() ● Set the year
(optionally month and
day)
● setHours() ● Set the hour (0-23)
● setMilliseconds() ● Set the milliseconds
(0-999)
● setMinutes() ● Set the minutes (0-
59)
● setMonth() ● Set the month (0-11)
● setSeconds() ● Set the seconds (0-
59)
● setTime() ● Set the time
(milliseconds since
Mathematical function:Example
Math.random();
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10);
</script>
</body>
</html>
DOM JavaScript
Document Object Model
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Hello World!";
</script>
● Method ● Description
● document.getElementById(id) ● Find an element by element id
● document.getElementsByTagName(na ● Find elements by tag name
me)
● document.getElementsByClassName(n ● Find elements by class name
ame)
getElementById
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p id="intro">Finding HTML Elements by Id</p>
<p>This example demonstrates the <b>getElementsById</b> method.</p>
<script>
const element = document.getElementById("intro");
</script>
</body>
</html>
getElementsByTagName/ getElementsByClassName
• const element = document.getElementsByTagName("p");
• const x = document.getElementsByClassName("intro");
Tag childNodes
<!DOCTYPE html>
<html>
<body>
<!-- This is a comment node! -->
To find all HTML elements that match a specified CSS selector (id, class names, types,
attributes, values of attributes, etc) use:
querySelectorAll() method.
This example:
const x = document.querySelectorAll("p.intro");
<!DOCTYPE html>
<html>
<body>
Finding HTML Eléments:
by HTML Object <h2>JavaScript HTML DOM</h2>
<p>Finding HTML Elements Using <b>document.forms</b>.</p>
Collections
<form id="frm1" action="/action_page.php">
First name: <input type="text" name="fname"
value="Donald"><br>
This example finds the form element Last name: <input type="text" name="lname"
with id="frm1", in the forms collection, and value="Duck"><br>
<br>
displays all element values: <input type="submit" value="Submit">
</form>
<p id="demo"></p>
<script>
const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length ;i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Changing HTML Elements
● Property ● Description
● element.innerHTML = new html content ● Change the inner HTML of an element
● element.setattribute = new value ● Change the attribute value of an HTML
element
● element.style.property = new style ● Change the style of an HTML element
The innerHTML Property
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
•
Change the innerHTML of an element:
Dynamic set to the innerHTML property
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current
date and time:</p>
<p id="demo"></p>
<script>
const d2 = new Date();
document.getElementById("demo").innerHTML = d2;
</script>
</body>
</html>
Changing the Value of an Attribute
• <!DOCTYPE html>
<html>
<body>
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jp
g";
</script>
</body>
</html>
Set Attribute: Change the attribute value of an HTML element
<!DOCTYPE html>
<html>
<style>
.democlass {
color: red;
}
</style>
<body>
<h1 id="myH1">The Element Object</h1>
<h2>The setAttribute() Method</h2>
<p>Click "Add Class" to add a class attribute to the h1 element:</p>
<script>
document.getElementById("myH1").setAttribute("class", "democlass");
</script>
</body>
style.property: Change the style of an HTML element
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Changing the HTML style:</p>
<p id="p1">Hello World!</p>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color = "blue";
document.getElementById("p2").style.fontFamily = "Arial";
document.getElementById("p2").style.fontSize = "larger";
</script>
</body>
</html>
Adding and Deleting Elements
● Method ● Description
● document.createElement(eleme ● Create an HTML element
nt)
● document.removeChild(element) ● Remove an HTML element
● document.appendChild(element) ● Add an HTML element
● document.replaceChild(new, old) ● Replace an HTML element
● document.write(text) ● Write into the HTML output
stream
Adding and Deleting Elements
const list = element.classList;
list.add("myStyle")
const list = element.classList;
list.remove("myStyle");
<!DOCTYPE html>
<html>
<style>
.myStyle {
background-color: coral;
padding: 16px;
}
</style>
<body>
<h1>The DOMToken Object</h1>
<h2>The add() Method</h2>
<button onclick="myFunction()">Add</button>
<p>Click "Add" to add the "myStyle" class to myDIV.</p>
<div id="myDIV">
<p>I am a myDIV.</p>
</div>
<script>
function myFunction() {
const list = document.getElementById("myDIV").classList;
list.add("myStyle");
}
</script>
Events :Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks
on an HTML element.
To execute code when a user clicks on an element, add JavaScript code to an
HTML event attribute:
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = displayDate;
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
Onload and onunload Events
<h1 onmouseover="style.color='red'"
onmouseout="style.color='black'">
Mouse over this text</h1>
</body>
</html>
•
The onmouseover
<!DOCTYPE html>
<html>
<body>
<div onmouseover="mOver(this)" onmouseout="mOut(this)"
style="background-
color:#D94A38;width:120px;height:20px;padding:40px;">
Mouse Over Me</div>
<script>
function mOver(obj) {
obj.innerHTML = "Thank You"
}
function mOut(obj) {
obj.innerHTML = "Mouse Over Me"
}
</script>
</body>
</html>
JavaScript HTML DOM EventListener
Syntax
element.addEventListener(event, function);
• The first parameter is the type of the event (like "click" or "mousedown" or any other
HTML DOM Event.)
• The second parameter is the function we want to call when the event occurs.
•
•
• You can easily remove an event listener by using
the removeEventListener() method.
• You can add many event handlers to one element.
•
•
JavaScript HTML DOM EventListener
• element.addEventListener("click", function()
{ alert("Hello World!"); });
element.addEventListener("click", myFunction);
function myFunction() {
alert ("Hello World!");
}
JavaScript HTML DOM EventListener
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript addEventListener()</h2>
<p>Uses addEventListener() method to attach a click event to a button.</p>
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click", displayDate);
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
Add an Event Handler to the window Object
• window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML = some
text;
});
Créer un calculatrice
1) Quand on click sur un chiffre il
devra changer de couleur
2)
3) Quand on click sur égale on
entendra une sonnerie légère
4)
5) Le résultat s’affiche selon une
animation donnée