JQuery detect if an item is already switched

I need jQuery to know that the element on my page is already switched or not.

I have a menu with XXX items.

When I click on an element, I use: $("#elementXX").slideToggle("slow"); (XX is the number)

If I click on another item, I need to know if the item is already switched, and I need to close this item before slideToggle is my new item.

How can i do this? I cannot check id by id like # element1, # element2, # element3 etc.

Thanks!

+4
source share
3 answers

You do not need to switch, slideUp and slideDown much better suited to your scenario. You can simply do:

 $("#clickedItem").click(function () { var wasVisible = $("#elementXX").is(":visible"); $("[id^=element]:visible").stop().slideUp("slow"); if (!wasVisible) { $("#elementXX").slideDown("slow"); } }); 
+2
source

You can set the data attributes to some value to see if it is switched. You can add attributes in html or use a script to add.

 $("#elementXX").data("toggled", "true"); 
+3
source

According to the official jQuery FAQ website:

 $("#toggleButton").click(function () { if ($("#elementXX").is(":visible")) { // toggled down } if ($("#elementXX").is(":hidden")) { // toggled up } }); 

https://learn.jquery.com/using-jquery-core/faq/how-do-i-determine-the-state-of-a-toggled-element/

0
source

All Articles