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 January 1, 1970)
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
• Le HTML DOM est un modèle d'objet standard on l’appel aussi une interface de
programmation pour le HTML, Il définit :
• En d'autres termes : Le HTML DOM est un standard pour savoir comment obtenir, modifier,
ajouter ou supprimer des éléments HTML.
DOM Properties / DOM Actions
<!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(name) Find elements by tag name
document.getElementsByClassName(name) Find elements by class name
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
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:
<h2>JavaScript HTML DOM</h2>
by HTML Object <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
The easiest way to get the content of an element is by using the innerHTML property.
The innerHTML property is useful for getting or replacing the content of HTML elements.
Change the inner HTML of an element
Static set to the innerHTML property.
<html>
<body>
<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.jpg
";
</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(element) Create an HTML element
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
</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 = somet
ext;
});
Créer un calculatrice
1) Quand on click sur un chiffre il
devra changer de couleur