Advertisement
RahmanIEN

main.js

Oct 25th, 2023 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.97 KB | Source Code | 0 0
  1. // URL API TheMealDB
  2. const apiUrl = "https://www.themealdb.com/api/json/v1/1/random.php";
  3.  
  4. // Pilih elemen di mana Anda ingin menampilkan detail makanan
  5. const mealDetails = document.getElementById("meal-details");
  6.  
  7. // Gunakan fetch untuk mengambil data dari API
  8. fetch(apiUrl)
  9.   .then((response) => response.json())
  10.   .then((data) => {
  11.     // Dapatkan detail makanan dari respons JSON
  12.     const meal = data.meals[0];
  13.  
  14.     // Buat tampilan detail makanan
  15.     const mealHTML = `
  16.                     <h2>${meal.strMeal}</h2>
  17.                     <img src="${meal.strMealThumb}" alt="${meal.strMeal}" width="280" hight="280">
  18.                     <p>Category: ${meal.strCategory}</p>
  19.                     <p style="text-align: justify;">Instructions: ${meal.strInstructions}</p>
  20.                 `;
  21.  
  22.     // Tampilkan detail makanan di dalam elemen meal-details
  23.     mealDetails.innerHTML = mealHTML;
  24.   })
  25.   .catch((error) => {
  26.     console.error("Error fetching data:", error);
  27.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement