Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. 'use strict';
  2.  
  3. let game_list = new Array();
  4. // let game_list = [];
  5.  
  6. const verwerkSpellen = function(jsonObject) {
  7. let html_options = '';
  8. console.log(jsonObject);
  9.  
  10.  
  11.  
  12. for (let spel of jsonObject) {
  13. addGame2Select(spel);
  14.  
  15. game_list["" + spel.gameId] = spel.image
  16. }
  17.  
  18. console.log(game_list);
  19. };
  20.  
  21. const addGame2Select = function(spel) {
  22. document.querySelector(
  23. '.js-select-spel',
  24. ).innerHTML += `<option value="${spel.gameId}">${spel.name}</option>`;
  25. };
  26.  
  27. const laadSpellen = function() {
  28. fetch('https://www.diero.be/MCT/JSON/spellen.json')
  29. .then(function(response) {
  30. if (!response.ok) {
  31. //error genereren
  32. //throw Error("Er is een progleem opgetreden: " + response.status);
  33. throw Error(
  34. `Er is een progleem opgetreden: ${response.status}`,
  35. );
  36. } else {
  37. // verwerk iets
  38. return response.json();
  39. }
  40. })
  41. .then(function(jsonObject) {
  42. // verwerk jason
  43. verwerkSpellen(jsonObject);
  44. })
  45. .catch(function(error) {
  46. console.log(`Fout bij het verwerken json: ${error}`);
  47. });
  48. };
  49.  
  50. const toonImage = function(gameId){
  51. document.querySelector('.js-spel').innerHTML = `<img src="${game_list[gameId]}" alt="Prentje van het spel">`
  52. }
  53.  
  54. const init = function() {
  55. // Haal de spellen op
  56. laadSpellen();
  57.  
  58. const html_select = document.querySelector('.js-select-spel');
  59. html_select.addEventListener('input', function() {
  60. const selectedGameId = html_select.options[html_select.selectedIndex].value;
  61. toonImage(selectedGameId);
  62. });
  63. };
  64.  
  65. document.addEventListener('DOMContentLoaded', init);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement