1

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.

J.Zil
  • 2,397
  • 7
  • 44
  • 78
  • try this http://stackoverflow.com/questions/2416803/jquery-contains-selector-to-search-for-multiple-strings/2417076#2417076 – Dru Jul 30 '12 at 08:45

5 Answers5

3
$('#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();
    }
});
Summoner
  • 1,397
  • 1
  • 9
  • 9
  • The searching bit is perfect, but...how can I add the expanding bit back to the answers? Before they toggled when clicked. – J.Zil Jul 30 '12 at 08:50
  • Just use the same click event you were using earlier. – Summoner Jul 30 '12 at 08:54
  • Sorry, I dont know JS too well. I tried to add .find('EM').slideToggle(); to my script to make it toggle, but it doesnt work: http://jsfiddle.net/pT6dB/60/ – J.Zil Jul 30 '12 at 09:08
  • I tried your approach but it broke the searching. On yours the search doesnt work either. Does the toggling conflict with the searching perhaps? – J.Zil Jul 30 '12 at 09:26
  • Sorry, I didn't test it. This should be working properly: http://jsfiddle.net/pT6dB/62/ – Summoner Jul 30 '12 at 09:31
0

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();
    });

Demo

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.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
0

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.

Simon Wang
  • 2,843
  • 1
  • 16
  • 32
0

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?

Community
  • 1
  • 1
Snow Blind
  • 1,164
  • 7
  • 12
0

Try that it also checks if you enter more then 1 word which are not sequential in the sentence.

Pavel Staselun
  • 1,970
  • 1
  • 12
  • 24