Using Javascript/jQuery to find similar SELECT options

Viewed 58

I'm currently using the .filter() method to match the value of an INPUT field (prjName) to an option in a SELECT field (prjList). This only works when the entered value is an exact match for the option text:

$("select[title='Projects List'] option").filter(function()
{
  return $(this).text() === prjName 
}).prop('selected', true);

So this code attempts to choose the first option in prjList that matches the text typed into prjName. No problems there, working as expected.

I have a second SELECT field to the side, let's call it prjSimilar. I can dynamically add options to prjSimilar. No problems there. What I'm trying to do is - in addition to an exact match - find anything similar to the value of prjName, then add it as a new option in prjSimilar. Essentially a keyword search. For example, if the user types "RED VOLKSWAGEN BEETLE" into the text box, I want to find any option in prjList that contains one of those three words, adding each one to prjSimilar as it finds it.

So far I've tried variations of .filter() and indexOf but I'm not having any success.

What Javascript/jQuery method can be used to find similar items in a list?

1 Answers
Related