Skip to content
Advertisement

Hide a parent div when filtering

I have timeline with a filter that allows users to show/hide events. The way the CSS is formatted requires that the entire parent (“.timeline”) be hidden if no matches were made in the filter, otherwise there’s a border that remains visible:

JSFiddle to see example

JavaScript
JavaScript

I tried including $('.timeline').hide().filter($filteredResults).show(); to the last line, which works at first, but then when you uncheck boxes none of the elements reappear.

Is there some sort of if/then statement I should be using? Like, if matched = false { document.getElementsByClassName("timeline").style.display = "none"; };

Advertisement

Answer

This is the Javascript you need

JavaScript

I added an if statement at the end to check if all events are hidden, by looping through all events and checking their visibility with if ($(this).is(":visible")). If they are all hidden it hides the timeline, if not the timeline stays put.

At the start of your function I added a line to make sure the timeline is visible before starting the visibility loop or else all events will be hidden and the code won’t work as $(this).is(":visible") will always return false.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement