Skip to content
Advertisement

Emptying value doesn’t return original filtered options

I’m trying to get all the original li’s back once I clear the input. The problem is it .value = ” clears the input but the filter is still applied.

I’d appreciate help on this, it’s driving me crazy.

(I removed the CSS but you can still get a pretty good idea, thx)

JavaScript
JavaScript

Advertisement

Answer

The filter itself is not “applied”, it’s the consequences of it that are.

When you look at your code, you’ll find that you have, at a point, set to display: none the <li> that were not matching your filter.

Your clear() function reset the value of the input, without reversing the changes made to the <li> that you did by applying display: none.

You have to make your clear() function also remove the display: none out of all <li> stored in your array by iterating through that same array and reversing the changes made to the display property.

JavaScript

Instead of using ul.getElementsByTagName("li"), you should use as I did above ul.children, which stores the childrens of the selected parent into an array, and the childrens of an <ul> are essentially all <li>, so.

The code above works and clear out the filter as you wanted to. I would recommend you to not use var, only const, let that will prevent many errors while manipulating variables throughout your code overall. Please consider them.

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