1

Here's my HTML,

{% for loop several times %}

<a href="#" class="showhide">Button</a><br />
<div class="dropdown">
    <a href="#">Update</a>
    <a href="#">Delete</a>
</div>

{% endfor %}

Also,

$(document).ready(function () {
$('.showhide').click(function() {
    $('.dropdown').toggle();
});
});
$(document).mouseup(function(e) {
var container = $(".dropdown");
if(container.is(':visible')&&!$(e.target).closest('.dropdown').length){
    container.hide();}
});
});

Divs are opening & closing fine. But the problem is that since I've a for loop, "Button" is appearing several times on one screen & if I'm clicking on just one button all other buttons also opening & closing the the divs with just one click.

How can I make them separately open the divs?

1 Answer 1

1

you need to target only the next .dropdown to each .showhide button so you can use $(this).nextAll('.dropdown:lt(1)')

$(document).ready(function () {
$('.showhide').click(function(e) {
$(this).nextAll('.dropdown:lt(1)').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="showhide">Button</a><br />
<div class="dropdown">
<a href="#">Update</a>
<a href="#">Delete</a>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Sir, everything has stopped working after adding this !
Thank You Sir, Working perfectly fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.