Reputation: 2449
This is my example code: http://jsfiddle.net/pT6dB/
I have a bit of a problem. There are two example answers there:
"How round is the Earth?" and "How rectangular is paper?"
If I type the search term "round Earth" I would expect it to show "How round is the Earth?" as a result but it doesnt.
I think I need to change the query so it says the equalivant of which questions contain both "round" and "Earth".
Would this be an easy modification? This is example code from a site so I wouldnt know where to start unfortunatly.
Upvotes: 1
Views: 172
Reputation: 2010
Try that it also checks if you enter more then 1 word which are not sequential in the sentence.
Upvotes: 0
Reputation: 1164
What you're looking for is full-text search
in javascript and contains
is not enough. But you can do something like the accepted answer of this question: text search in javascript?
Upvotes: 0
Reputation: 2943
I suppose you are asking "Would this be an easy modification?" The answer is NO. :)
You are trying to make an Full-Text Search without database but just jQuery, lets not talking about more complex situation but at least you need to use split to split those words user typed in with space and check them one by one.
Upvotes: 0
Reputation: 1407
$('#search').keyup(function(e) {
var s = $(this).val().trim();
// show all results
$('#result LI').show();
// split the search into words
var keywords = s.split(' ');
// loop over the keywords and if it's not in a LI, hide it
for(var i=0; i<keywords.length; i++) {
$('#result LI:not(:contains('+keywords[i]+'))').hide();
}
});
Upvotes: 3
Reputation: 129792
You would need to perform a search for each word, rather than for the string "round Earth", which doesn't exist in any of the items:
var words = s.split(' ');
$('#result LI').hide();
$(words).each(function() {
$('#result LI:contains(' + this + ')').show();
});
Of course there are conceivable complications here; all we've done is to adjust the script to cover one more specific scenario. But that may be good enough for your purposes.
Upvotes: 0