Exercices_JavaScript_Fonctions_et_Onclick
Exercices_JavaScript_Fonctions_et_Onclick
Code attendu :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exercice 1</title>
<script>
function afficherMessage() {
alert("Bonjour, bienvenue !");
}
</script>
</head>
<body>
<button onclick="afficherMessage()">Cliquez ici</button>
</body>
</html>
Code attendu :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exercice 2</title>
<script>
function changerTexte() {
document.getElementById("monParagraphe").innerText = "Texte modifié avec
JavaScript !";
}
</script>
</head>
<body>
<p id="monParagraphe">Ceci est un paragraphe.</p>
<button onclick="changerTexte()">Modifier le texte</button>
</body>
</html>
Code attendu :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exercice 3</title>
<style>
#maDiv {
width: 200px;
height: 100px;
background-color: lightgray;
}
</style>
<script>
function changerCouleur() {
document.getElementById("maDiv").style.backgroundColor = "blue";
}
</script>
</head>
<body>
<div id="maDiv"></div>
<button onclick="changerCouleur()">Changer la couleur</button>
</body>
</html>
Code attendu :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exercice 4</title>
<script>
function afficherValeur() {
const valeur = document.getElementById("monInput").value;
alert("Vous avez saisi : " + valeur);
}
</script>
</head>
<body>
<input type="text" id="monInput" placeholder="Entrez quelque chose">
<button onclick="afficherValeur()">Afficher la valeur</button>
</body>
</html>
Code attendu :
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exercice 5</title>
<script>
let compteur = 0;
function incrementerCompteur() {
compteur++;
document.getElementById("compteur").innerText = compteur;
}
</script>
</head>
<body>
<p>Compteur : <span id="compteur">0</span></p>
<button onclick="incrementerCompteur()">Cliquer ici</button>
</body>
</html>