I’m working on my E-commerce project. Here is something I am struggling to figure out…
If clicking on the “i” icon, a modal should open.
Code snippet
JavaScript
x
43
43
1
2
const products = [
3
{
4
id: 1,
5
title: 'Pens',
6
img: '/assets/images/Stationery Items/d-pens.jpg',
7
description: 'Pens',
8
rate: 4.2,
9
},
10
{
11
id: 2,
12
title: 'Notebooks',
13
img: '/assets/images/Stationery Items/d-notebooks.jpg',
14
description: 'Notebooks',
15
rate: 3.4,
16
},
17
{ }
18
]
19
20
21
function showProduct(product) {
22
return `
23
24
<div class="box-wrap border">
25
<img src="${product.img}" class="rounded d-block">
26
<div class="body">
27
<h5>${product.title}</h5>
28
<div class="d-flex justify-content-between">
29
<a href="#"><i class="fas fa-star"></i></a>
30
<a href="#"><i class="fas fa-info"></i></a>
31
</div>
32
</div>
33
</div>
34
`
35
}
36
37
function productList() {
38
document.getElementById('products').innerHTML = `
39
<div class="owl-carousel">
40
${products.map(showProduct).join('')}
41
</div>
42
`
43
}
Advertisement
Answer
You can use Bootstrap with modal:
JavaScript
1
31
31
1
const products = [
2
{
3
id: 1,
4
title: 'Pens',
5
img: '/assets/images/Stationery Items/d-pens.jpg',
6
description: 'Pens',
7
rate: 4.2,
8
},
9
{
10
id: 2,
11
title: 'Notebooks',
12
img: '/assets/images/Stationery Items/d-notebooks.jpg',
13
description: 'Notebooks',
14
rate: 3.4,
15
},
16
];
17
18
products.map((v, k) => {
19
$('#sliders').append('<button type="button" class="btn btn-primary openModal" data-key="'+ k +'">'+ v.title +'</button>')
20
})
21
22
$(document).on('click', 'button.openModal', function () {
23
var productKey = $(this).data('key');
24
var product = products[productKey];
25
if(product){
26
$('#exampleModal').modal('toggle');
27
$('#exampleModal #exampleModalLabel').html(product.title);
28
var content = '<div>Title: '+ product.title + '</div><div>Description: ' + product.description+'</div><div>Rate: '+product.rate+'</div>';
29
$('#exampleModal .modal-body').html(content);
30
}
31
})
JavaScript
1
32
32
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
5
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
6
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
7
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet"/>
8
</head>
9
<body >
10
<!-- Button trigger modal -->
11
<div id="sliders"></div>
12
13
<!-- Modal -->
14
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
15
<div class="modal-dialog">
16
<div class="modal-content">
17
<div class="modal-header">
18
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
19
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
20
<span aria-hidden="true">×</span>
21
</button>
22
</div>
23
<div class="modal-body"></div>
24
<div class="modal-footer">
25
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
26
<button type="button" class="btn btn-primary">Save changes</button>
27
</div>
28
</div>
29
</div>
30
</div>
31
</body>
32
</html>