I want to toast messages for all those cards. but it is showing for the first card only. I have attached a view of my page where I want to add a toast message to view the details of the card if a user is not logged in.
JavaScript
x
12
12
1
document.getElementById("toastbtn").onclick = function() {
2
3
var toastElList = [].slice.call(document.querySelectorAll('.toast'))
4
var toastList = toastElList.map(function(toastEl) {
5
6
return new bootstrap.Toast(toastEl)
7
})
8
toastList.forEach(toast => toast.show())
9
}
10
11
12
JavaScript
1
48
48
1
<section class="details-card">
2
3
<div class="container">
4
<div class="row">
5
{% for homes in home %}
6
<div class="col-md-4 mb-4">
7
<div class="card-content">
8
<div class="card-img">
9
<img src="{{ homes.coverImg.url }}" alt="Cover Image">
10
<span><h4>{{ homes.pricePerMonth }}Taka</h4></span>
11
</div>
12
<div class="card-desc">
13
<p class="small mb-1"> <i class="fas fa-map-marker-alt mr-2"></i>{{homes.address}}</p>
14
<h3>{{ homes.title}}</h3>
15
{% if request.user.is_authenticated %}
16
<a href="{% url 'HomeDetails' homes.id %}" class="btn btn-md btn-primary hover-top">Details</a>
17
{% else %}
18
<button type="button" class="btn btn-primary" id="toastbtn">XDetails</button>
19
20
21
{% endif %}
22
</div>
23
</div>
24
</div>
25
{% endfor %}
26
</div>
27
</div>
28
29
</section>
30
31
<!-- Alert Message Popup-->
32
<!--bottom-0 end-0 p-3-->
33
<div class="position-fixed top-50 start-50 translate-middle p-3" style="z-index: 11">
34
<div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true">
35
<div class="toast-header">
36
<img src="({% static 'img/icon.png' %})" class="rounded me-2" alt="...">
37
<strong class="me-auto">My Second Home</strong>
38
<small>0.1s ago</small>
39
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
40
</div>
41
<div class="toast-body">
42
Hello!<br>You need to login first to see details.
43
<div class="mt-2 pt-2 border-top">
44
<a class="btn btn-primary btn-sm" href="{% url 'login' %}">Sign In</a>
45
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="toast">Close</button>
46
</div>
47
</div>
48
</div>
Advertisement
Answer
This solution is collected from Gahan Vig!!
JavaScript
1
9
1
function showToast() {
2
3
var toastElList = [].slice.call(document.querySelectorAll('.toast'))
4
var toastList = toastElList.map(function(toastEl) {
5
6
return new bootstrap.Toast(toastEl)
7
})
8
toastList.forEach(toast => toast.show())
9
}
JavaScript
1
1
1
<button type="button" class="btn btn-primary" onclick="showToast()">XDetails</button>