I'm trying to make the entire DIV clickable and got it working. However, I want to make an exception to the dropdown menu. Clicking this dropdown button
shouldn't open up next.html
. It currently opens up the dropdown but also quickly opens up next.html
. How do I go about this?
Demo: https://jsfiddle.net/40wntLyo/
<div class="project__body" onclick="location.href='next.html';">
<div class="row">
<div class="col-10">
<h1>Title of the DIV</h1>
</div>
<div class="col-2 text-right">
<div class="dropdown">
<button type="button" class="dropdown-btn dropdown-toggle" data-toggle="dropdown">Menu</button>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Edit</a>
<a class="dropdown-item" href="#">Delete</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<p>There's more content here in this div. There's more content here in this div. There's more content here in this div. </p>
</div>
</div>
Add a JS function to the dropdown's parent div which is called on click:
<div class="col-2 text-right" onclick="processClick()">
// Dropdown content here
</div>
<script>
function processClick(e) { e.stopPropagation() }
</script>
First thing you have to remove inline javascript event and try below exmaple:
jQuery:
$('.dropdown').click(function(event){
event.stopPropagation();
$('.dropdown-menu').toggle();
})
$('.project__body').click(function(){
window.location.href="next.html"
})
html:
<div class="project__body">
<div class="row">
<div class="col-10">
<h1>Title of the DIV!</h1>
</div>
<div class="col-2 text-right">
<div class="dropdown">
<button type="button" class="dropdown-btn dropdown-toggle" data-toggle="dropdown">Menu</button>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Edit</a>
<a class="dropdown-item" href="#">Delete</a>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<p>There's more content here in this div. There's more content here in this div. There's more content here in this div. </p>
</div>
</div>
The following snippet should work
Move the click event to the parent dropdown container and it will also prevent redirection on click of dropdown items
$('.dropdown').click(function(event) { $(this).children(".dropdown-menu").toggle() event.stopPropagation(); })
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <div class="project__body" onclick="location.href='next.html';"> <div class="row"> <div class="col-10"> <h1>Title of the DIV!</h1> </div> <div class="col-2 text-right"> <div class="dropdown"> <button type="button" class="dropdown-btn dropdown-toggle" data-toggle="dropdown">Menu</button> <div class="dropdown-menu dropdown-menu-right"> <a class="dropdown-item" href="#">Edit</a> <a class="dropdown-item" href="#">Delete</a> </div> </div> </div> </div> <div class="row"> <div class="col-12"> <p>There's more content here in this div. There's more content here in this div. There's more content here in this div. </p> </div> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.