Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
<!DOCTYPE html>
<html>
<head>
  <title>JSON Data in HTML Cart</title>
  <style>
    .cart {
      border: 1px solid #ccc;
      padding: 10px;
      margin: 10px;
    }

    .cart-item {
      margin-bottom: 5px;
    }

    .cart-item strong {
      display: inline-block;
      width: 80px;
    }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="script.js"></script>
</head>
<body>
  <h1>JSON Data in HTML Cart</h1>
  <div id="cart-container"></div>
</body>
</html>

$(document).ready(function() {
  // Ajax request to get JSON data from API
  $.ajax({
    url: "https://api.example.com/data", // Replace with the actual API endpoint URL
    method: "GET",
    success: function(response) {
      displayCart(response);
    },
    error: function() {
      console.log("Error: Failed to retrieve data from the API.");
    }
  });

  // Function to display JSON data as an HTML cart
  function displayCart(data) {
    var cartContainer = $("#cart-container");
    cartContainer.empty();

    if (data.length === 0) {
      cartContainer.text("No items in the cart.");
      return;
    }

    var cart = $("<div class='cart'></div>");

    // Process and display the JSON data as an HTML cart
    $.each(data, function(index, item) {
      var cartItem = $("<div class='cart-item'></div>");

      var name = $("<strong>Name:</strong>");
      var nameValue = $("<span>").text(item.name);
      cartItem.append(name).append(nameValue);

      var price = $("<strong>Price:</strong>");
      var priceValue = $("<span>").text("$" + item.price);
      cartItem.append(price).append(priceValue);

      // Add additional properties as needed, e.g., quantity, description, etc.

      cart.append(cartItem);
    });

    cartContainer.append(cart);
  }
});