Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. fetch("./xcar6.json")
  2. .then(function(resp) {
  3. return resp.json();
  4. })
  5. .then(function(data) {
  6. document.getElementById('allcars').addEventListener('click', allCars);
  7.  
  8. var output = '';
  9.  
  10. function allCars(){
  11. for (var i = 0; i < data.xCars.length; i++) {
  12. output += `
  13. <h3>${data.xCars[i].model}</h3>
  14. <li>Name: ${data.xCars[i].name}</li>
  15. <li>Age: ${data.xCars[i].age}</li>
  16.  
  17. <img src="${data.xCars[i].img}" width="200px" height="200px"></img>
  18.  
  19. <br>
  20.  
  21. <button id="openmodel">Show Model</button>
  22. `;
  23. }
  24.  
  25. document.getElementById('cars').innerHTML = output;
  26. }
  27. });
  28.  
  29. {
  30. "xCars": [
  31. {
  32. "id": 1,
  33. "name": "Xcar-1",
  34. "model": "Cabrio",
  35. "img": "img/cabrio.jpg",
  36. "properties": {
  37. "fuel": "Electric",
  38. "price": 5000,
  39. "year": 2016
  40. }
  41. },
  42. {
  43. "id": 2,
  44. "name": "Xcar-2",
  45. "model": "Coupe",
  46. "img": "img/coupe.jpg",
  47. "properties": {
  48. "fuel": "Benzine",
  49. "price": 7500,
  50. "year": 2017
  51. }
  52. },
  53. {
  54. "id": 3,
  55. "name": "Xcar-3",
  56. "model": "Hatchback",
  57. "img": "img/hatchback.png",
  58. "properties": {
  59. "fuel": "Diesel",
  60. "price": 2500,
  61. "year": 2018
  62. }
  63. },
  64. {
  65. "id": 4,
  66. "name": "Xcar-4",
  67. "model": "SUV",
  68. "img": "img/SUV.jpg",
  69. "properties": {
  70. "fuel": "Electric",
  71. "price": 10000,
  72. "year": 2019
  73. }
  74. }
  75. ]
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement