Reputation: 71
i have one paragraph and one button inside a div tag. I want to use onlick="" method on div tag but not on the button which is inside that div tag. How can I do this.
<div class="alert alert-secondary alert-dismissible fade show" role="alert" data-bs-toggle="collapse" data-bs-target="#collapseExample{{data.id}}" aria-expanded="false" aria-controls="collapseExample">
<p>
<strong>{{ data.name }} / {{ data.messager_profile }}</strong> <span>{{ data.message_headline }}</span>
<br>
<span style="float: left;">8 days ago</span>
</p>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<br>
<div class="collapse" id="collapseExample{{data.id}}">
<div class="card card-body">
<strong>Detailed Message:-</strong> <span>{{ data.message }}</span>
</div>
</div>
</div>
Upvotes: 2
Views: 2550
Reputation: 56843
There is neither need for jQuery here, nor for changing the behaviour of the button click. Instead, inside your click handler, check for the element clicked:
document
.querySelector('.alert.alert-secondary') // select the div
.addEventListener('click', function(e) { // register a click listener
const clickedElement = e.target; // the event object holds information about the clicked element
if (clickedElement.tagName !== 'BUTTON') { // make sure you don't react to button clicks bubbling to the div
console.log('div clicked');
}
});
<div class="alert alert-secondary alert-dismissible fade show" role="alert" data-bs-toggle="collapse" data-bs-target="#collapseExample{{data.id}}" aria-expanded="false" aria-controls="collapseExample">
<p>
<strong>{{ data.name }} / {{ data.messager_profile }}</strong> <span>{{ data.message_headline }}</span>
<br>
<span style="float: left;">8 days ago</span>
</p>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">x</button>
<br>
<div class="collapse" id="collapseExample{{data.id}}">
<div class="card card-body">
<strong>Detailed Message:-</strong> <span>{{ data.message }}</span>
</div>
</div>
</div>
Upvotes: 0
Reputation: 738
What you are looking for is the event.stopPropagation()
method. This will prevent the event from bubbling up to the parent container. Here is an example using your HTML. I used jQuery since Bootstrap is involved.
$(".alert.alert-secondary").click(function() {
console.log("Parent container");
})
$(".btn-close").click(function(e) {
e.preventDefault();
e.stopPropagation();
console.log("Button");
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div class="alert alert-secondary alert-dismissible fade show" role="alert" data-bs-toggle="collapse" data-bs-target="#collapseExample{{data.id}}" aria-expanded="false" aria-controls="collapseExample">
<p>
<strong>{{ data.name }} / {{ data.messager_profile }}</strong> <span>{{ data.message_headline }}</span>
<br>
<span style="float: left;">8 days ago</span>
</p>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">button</button>
<br>
<div class="collapse" id="collapseExample{{data.id}}">
<div class="card card-body">
<strong>Detailed Message:-</strong> <span>{{ data.message }}</span>
</div>
</div>
</div>
Upvotes: 2