-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
37 lines (31 loc) · 1.5 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const productContainer = document.getElementById('productContainer');
fetch('products.xml')
.then(response => response.text())
.then(data => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(data, 'application/xml');
const products = xmlDoc.querySelectorAll('product');
products.forEach(productNode => {
const product = {
name: productNode.querySelector('name').textContent,
image: productNode.querySelector('image').textContent,
description: productNode.querySelector('description').textContent,
price: productNode.querySelector('price').textContent,
link: productNode.querySelector('link').textContent
};
const productElement = document.createElement('div');
productElement.classList.add('product');
productElement.innerHTML = `
<img src="${product.image}" alt="${product.name}">
<h2>${product.name}</h2>
<p>${product.description}</p>
<span class="price">${product.price}</span>
<button class="view-product">View Product</button>
`;
const viewProductButton = productElement.querySelector('.view-product');
viewProductButton.addEventListener('click', () => {
window.location.href = product.link;
});
productContainer.appendChild(productElement);
});
});