I am making an ad card using bootstrap, I’ve placed the card inside <a>
element so that when the user clicks the card, the whole card will act as a link.
I have a favorite link that marks the ad as a favorite for the user. The problem is, since the whole card is a link, whenever I click the fav button, the parent link is clicked which is meant to go to the page of the ad, I want to stop that behavior and instead run whatever function favorite button has assigned to it.
Basically I want to stop the behavior of parent link.
My HTML for card:
JavaScript
x
17
17
1
<a href="" class="text-dark text-decoration-none">
2
<div class="card">
3
<div class="card-img-top">
4
<img src="{{ url_for('static', filename='images/test/table-with-various-goods-in-shop.jpg') }}" alt="Card Title">
5
</div>
6
<div class="card-body">
7
<h5 class="card-title">Price</h5>
8
<h6 class="card-subtitle mb-2 text-muted">Title</h6>
9
<span class="fa fa-heart-o favorite-ad-card-btn" id="favoriteAdCardBtn"></span>
10
<div class="footer mt-4">
11
<small class="float-start text-muted">Location</small>
12
<small class="float-end text-muted">Time</small>
13
</div>
14
</div>
15
</div>
16
</a>
17
My JS present in an external file:
JavaScript
1
26
26
1
let favoriteBtn = document.getElementById("favoriteAdCardBtn");
2
favoriteBtn.addEventListener("mouseover", mouseOverFavoriteBtn);
3
favoriteBtn.addEventListener("mouseout", mouseOutFavoriteBtn);
4
favoriteBtn.addEventListener("click", onClickFavoriteBtn);
5
6
function mouseOverFavoriteBtn(e) {
7
if (e.target.classList.contains("fa-heart-o")) {
8
e.target.classList.remove("fa-heart-o");
9
e.target.classList.add("fa-heart");
10
}
11
}
12
13
function mouseOutFavoriteBtn(e) {
14
if (!(e.target.classList.contains("text-danger"))) {
15
e.target.classList.remove("fa-heart");
16
e.target.classList.add("fa-heart-o");
17
}
18
}
19
20
function onClickFavoriteBtn() {
21
e = window.event || e;
22
if (this == e.target) {
23
console.log("Fav btn clicked");
24
}
25
}
26
Advertisement
Answer
You can prevent the default event if the button is clicked inside the anchor element’s click event handler function like the following way:
JavaScript
1
6
1
document.querySelector('a.text-dark.text-decoration-none').addEventListener("click", function(e){
2
if(e.target.id == 'favoriteAdCardBtn'){
3
e.preventDefault();
4
}
5
});
6
Demo:
JavaScript
1
31
31
1
let favoriteBtn = document.getElementById("favoriteAdCardBtn");
2
favoriteBtn.addEventListener("mouseover", mouseOverFavoriteBtn);
3
favoriteBtn.addEventListener("mouseout", mouseOutFavoriteBtn);
4
favoriteBtn.addEventListener("click", onClickFavoriteBtn);
5
6
function mouseOverFavoriteBtn(e) {
7
if (e.target.classList.contains("fa-heart-o")) {
8
e.target.classList.remove("fa-heart-o");
9
e.target.classList.add("fa-heart");
10
}
11
}
12
13
function mouseOutFavoriteBtn(e) {
14
if (!(e.target.classList.contains("text-danger"))) {
15
e.target.classList.remove("fa-heart");
16
e.target.classList.add("fa-heart-o");
17
}
18
}
19
20
function onClickFavoriteBtn(e) {
21
e = window.event || e;
22
if (this == e.target) {
23
console.log("Fav btn clicked");
24
}
25
}
26
27
document.querySelector('a.text-dark.text-decoration-none').addEventListener("click", function(e){
28
if(e.target.id == 'favoriteAdCardBtn'){
29
e.preventDefault();
30
}
31
});
JavaScript
1
17
17
1
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
2
<a href="" class="text-dark text-decoration-none">
3
<div class="card">
4
<div class="card-img-top">
5
<img src="{{ url_for('static', filename='images/test/table-with-various-goods-in-shop.jpg') }}" alt="Card Title">
6
</div>
7
<div class="card-body">
8
<h5 class="card-title">Price</h5>
9
<h6 class="card-subtitle mb-2 text-muted">Title</h6>
10
<span class="fa fa-heart-o favorite-ad-card-btn" id="favoriteAdCardBtn"></span>
11
<div class="footer mt-4">
12
<small class="float-start text-muted">Location</small>
13
<small class="float-end text-muted">Time</small>
14
</div>
15
</div>
16
</div>
17
</a>