jQuery toggle .click() function
15,353
Solution 1
Use .toggle()
, not .click()
.
Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks.
Solution 2
try toggle
$(".paneltop").toggle(function(){
//first click happened
},function(){
//second click happened
});
Related videos on Youtube
Author by
A_funs
Updated on June 04, 2022Comments
-
A_funs over 1 year
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); }) })
-
genesis about 12 yearsCan someone explain his/her downvote so I can improve my answer?
-
Matt Ball about 12 yearsThat would be my downvote - sorry, I was going to comment but got sidetracked. I downvoted your original answer, because it was just plain wrong. Why didn't you just edit it? Anyway, it amounted to the same information as my answer, after I'd already posted (and downvoted your wrong answer).
-
A_funs about 12 yearsAlso, is that technically what I wanted? a callback? or is it called something else?
-
Matt Ball about 12 yearsI don't think "callback" is the term for what you wanted. A callback (in JavaScript, anyway) is a function to be executed at a later time. The 3 anonymous functions in your code are all examples of callbacks. The first one is a callback which is executed when the DOM is ready.
-
genesis about 12 years@MattBall: I have deleted it because it was downvoted long time before and chances to get upvoted is very small.
-
Matt Ball about 12 yearsA brief write-up on JS callbacks: recurial.com/programming/… (the Wikipedia article might be a bit opaque...)
-
Matt Ball about 12 yearsThere were 2 downvotes. One of them was mine. I am happy to remove downvotes when an answer is fixed. I imagine that your other downvoter would do the same.
-
genesis about 12 years@MattBall: Okay. So what's the reason for still keeping your downvote on my post now, though?
-
EsaulFarfan over 3 years.toggle() is deprecated since v.1.8 - Use conditionals instead (if..else..)