Forums › Forums › Search & Filter Pro › Ajax is clearing added classes (after sf:ajaxfinish)
- This topic has 16 replies, 3 voices, and was last updated 5 years, 1 month ago by
Ross.
-
Ross Moderator(Private) May 8, 2020 at 8:44 am #243114
Hi Bond
If I’m following correctly I see the error.
The approach your opening post I think was pretty close but there needs to be some additional logic.
Essentially from this code:
$(document).ready(function(){ $('.searchandfilter ul li li.sf-level-0 > input').click(function(){ $(this).parent("li").toggleClass('open'); }); }); jQuery(document).on("sf:ajaxfinish", ".searchandfilter", function(){ console.log("ajax finish"); $('.searchandfilter ul li li.sf-level-0 > input').click(function(){ $(this).parent("li").toggleClass('open'); }); });
You attach the events correctly, which basically says when there is a click, toggle class “open”.
However, when
sf:ajaxfinish
completes, the “open” class is removed, this is standard behaviour as our ajax request won’t contain the class you added.In
sf:ajaxfinish
, you do correctly setup the click event again, but you also need to restore the “open” class to whatever it was on first.So when you click an item, you can record the class of the element in variable, eg
.level-0 .sf-item-41
and then onsf:ajaxfinish
, attach the “open” class to that item, before doing anything else like adding the click handler again.Does that make sense?
Thanks
-
AuthorPosts