Carpy
Carpy

Reputation: 1074

Slidetoggle with a close button - jQuery

I'm using the slidetoggle function to open & close a box when clicking on a link. I want to be able to add a "close" button (link) inside that box aswell as being able to close it via the slidetoggle trigger. How would i do this?

This is my jQuery:

$(".popup-b").hide(); 
$(".popup-l").click(function(){
    $(this).toggleClass("active").next().slideToggle(0);
    return false;
});

My HTML

<a class="popup-l" href="#">Areas</a>
<div id="area-p" class="popup-box">
    <div class="close-this"><a href="#">Close</a></div>
    <p>This is the popup box</p>
</div>

Upvotes: 1

Views: 3730

Answers (2)

Nick Brunt
Nick Brunt

Reputation: 10057

jQuery:

$(".popup-b").hide(); 
$(".popup-l").click(function(){
    $(this).toggleClass("active").next().slideToggle(0);
    return false;
});
$(".close-popup").click(function() {
  $(this).parent().prev().toggleClass("active").next().slideToggle(0);
);

HTML

<a class="popup-l" href="#">Areas</a>
<div id="area-p" class="popup-box">
    <div class="close-this"><a href="#" class="close-popup">Close</a></div>
    <p>This is the popup box</p>
</div>

Upvotes: 0

Erick Petrucelli
Erick Petrucelli

Reputation: 14872

Check out this Fiddle to see it working.

$(".popup-content").hide(); 
$(".popup-title").click(function(){
    $(this).toggleClass("active").next().slideToggle(0);
    return false;
});
$(".popup-content .close-this").click(function(){
    $(".popup-title", $(this).parents(".popup-box")).click();
    return false;
});

This way the active class will be setted when the close link is used too.

You can also have many .popup-box on the same page.

Upvotes: 1

Related Questions