Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggle .click() function

I am unsure if this is a "callback' that I need or not, but basically I am trying to emulate the reversal of the function like on the .hover() effect with the .click() effect. Is this possible. So basically, if the user clicks again on the link, the reverse of what just happened, ie a div closing as opposed to opening, will happen. Is it possible to do this with .click()?

$(document).ready(function(){
    $('.content_accrd').css({"height":"0px"});
$('.paneltop').click( function() {
    $(this).css({"background-position":"0px -21px"})
    .siblings().animate({"height":"100px"}, 200)
}, function() {
$(this).css({"background-position":"0px 0px"})
    .siblings().animate({"height":"0px"}, 200);

})
})
like image 216
A_funs Avatar asked Jan 01 '26 01:01

A_funs


2 Answers

Use .toggle(), not .click().

Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks.

like image 58
Matt Ball Avatar answered Jan 02 '26 14:01

Matt Ball


try toggle

$(".paneltop").toggle(function(){
    //first click happened
},function(){
    //second click happened
});
like image 39
genesis Avatar answered Jan 02 '26 15:01

genesis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!