Skip to content
Advertisement

Hide elements that don’t match value from input field and keep only ones that have it

I’m doing a small project where I’m searching for words in collapsed elements. If found, some collapsed elements are opening to display the word.

The next thing I would like to achieve is to keep only the li that contain it and hide the others.

I’m not sure how to proceed further to achieve this. What I’ve tried, hides exactly the element found which in fact, I want to keep. I might need a loop but I’m not sure how to write it.

Everything happens here:

JavaScript

Can someone help me clarify this?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

You can get the current item and make all the items to hide first and then display the matched list item like,

JavaScript
JavaScript
JavaScript

This line el.parentNode.parentNode will select the respective <li> ... </li> element.

Advertisement