Skip to content
Advertisement

Search Filter Javascript – Search for multiple texts inside a cards and

I’m currently trying to make a dynamic search bar with JavaScript which can search inside cards, the title and multiple keywords.

I currently have something but it doesn’t seem to work properly. When I try searching for something even though the input value matches with the title or any of the keywords it still doesn’t show it.

For example, if one card contains “docker” in the title and the other card contains “docx” in the title, and I’d type 'do' it would show both but when I type ‘doc’, the docx card gets removed and it only shows the docker card.

Here I show only two cards, but in the project there are more of them:

JavaScript
JavaScript

It runs every time we type something in the input. el parameter is the input element.

I wanted to know if there’s something I could do to make it work while having this code or do I need to remove the code completely.

Thank you in advance.

Advertisement

Answer

The problem seems to be:

JavaScript

I’d suggest rewriting to use Array methods, as follows – with explanatory comments in the code – specifically using Array.prototype.some():

JavaScript
JavaScript
JavaScript

JS Fiddle demo.

However, I would personally rewrite the above to the following, with explanatory comments in the code:

JavaScript
JavaScript
JavaScript

JS Fiddle demo.

References:

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