Created
March 26, 2020 14:47
-
-
Save GravenilvecTV/21bf62eef60c2e2ec36315436cd7fd69 to your computer and use it in GitHub Desktop.
Correction TP 7/30 - Générateur de citations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Générateur de citations</title> | |
<meta charset="utf-8"/> | |
</head> | |
<body> | |
<h1 id="citation">Une variable c'est quelque chose qui peut varier</h1> | |
<img id="avatar" alt="Avatar de Graven" src="http://graven.yt/graven.jpg"> | |
<h1 id="nom">Graven</h1> | |
<button id="btn">Générer</button> | |
<!-- charger le code javascript --> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// une variable pour récuperer depuis mon fichier html, le composant qui porte l'id 'nom' | |
const url = "http://graven.yt/citations.json"; | |
let btn = document.getElementById('btn'); | |
let avatar = document.getElementById('avatar'); | |
let citation = document.getElementById('citation'); | |
let text = document.getElementById('nom'); | |
let citations = []; | |
// faire l'evenement lors d'un click bouton | |
btn.addEventListener('click', updatePage); | |
// recuperer toutes les citations depuis le lien graven.yt/citations.json | |
fetch(url).then((data) => { | |
data.json().then((data) => { | |
citations = data; | |
// affichage de citations | |
console.log(citations); | |
}); | |
}); | |
function updatePage() { | |
// choix au hasard d'une citation parmis la liste | |
let random = Math.floor(Math.random() * (citations.length - 0)); | |
let randomCitation = citations[random]; | |
// modification | |
text.innerText = randomCitation['nom']; | |
citation.innerText = randomCitation['citation']; | |
avatar.setAttribute('src', randomCitation['image']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment