Check if the element is hidden or not, but “else” is not working
Check if the element is hidden or not, but “else” is not working
I have a block, that when user clicks on it, some information appears (they are hidden by default). I want, when user clicks again on that block, the information will be closed again.
I use "if", that element has "hidden" class, do something. Those hidden classes should be removed after this, so it means if I write an "else" after if, it should work, but it is not working.
For the test I put an alert on else, but nothing happens, when I click on the block again, when the information is open.
if ($(".container-info").find(".content-holder").hasClass("hidden")) {
$(e.target).closest(".container-info").find(".hidden").removeClass("hidden");
} else {
alert("Im Working!");
}
working as expected - codepen.io/nagasai/pen/QxYaqg, did you provide event e in onclick function
– Naga Sai A
Jul 1 at 16:31
1 Answer
1
You should look at the jQuery toggle function:
http://api.jquery.com/toggle/
Example:
$( ".content-holder" ).click(function() {
$(this).closest(".container-info").toggle();
});
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That's not how else works. It will do the test once and then choose which path to take. If you change something inside the if, it won't cause the else to fire.
– Mark Meyer
Jul 1 at 16:29