I have this code
jQuery('.button').click(function() {
jQuery('.div1').animate({width: 'toggle'});
jQuery('.div2').animate({width: '100%'});
});
Works fine for once. But I want to bring back div2 on clicking again.
toggle would’ve worked but the problem is, I don’t want to hide div2 but just lower its width with animation.
What would be the correct code to do that?
Updated solution
Since
.togglewas deprecated in jQuery 1.8 and removed in 1.9, the go-to solution would now involve manually keeping track of clicks in order to decide what to do. A generalized drop-in replacement for toggle would be:Original answer
You can simply use
.toggle:On each click, this will toggle the visibility of
.div1completely and toggle the width of.div2between 50% and 100%.If you don’t want to show
.div1again after first hiding it, simply do