Reputation: 2449
I asked a question before but I need a bit more information. I found this great snippet for searching FAQ items:
I want to find a question containing "round and "Earth". However, if I type "round Earth" in looks specifically for "round Earth" which it cant find. I want it to be an "AND" query.
Would this be an easy modification?
Upvotes: 0
Views: 3515
Reputation: 929
Updated Fiddle to search case Insensitive.
http://jsfiddle.net/pT6dB/161/
$(document).ready(function() {
$('LI STRONG').click(function(e) {
e.preventDefault(); // disable text selection
$(this).parent().find('EM').slideToggle();
return false; // disable text selection
});
jQuery.expr[':'].Contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
jQuery.expr[':'].contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
$('#search').keyup(function(e) {
var s = $(this).val().trim();
if (s === '') {
$('#result LI').show();
return true;
}
$('#result LI:not(:contains(' + s + '))').hide();
$('#result LI:contains(' + s + ')').show();
return true;
});
}); // end document ready
#faq EM {
display:none;
}
#faq LI STRONG {
font-weight:normal;
color:#246;
text-decoration:underline;
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="faq">
<p><label>Search</label><br />
<input id="search" value="" size="30" /></p>
<p> </p>
<ul id="result">
<li><strong>How round is the Earth?</strong><br /><br /><em>
Very round.
<br /><br /></em></li>
<li><strong>How rectangular is paper?</strong><br /><br /><em>
Very rectangular.
<br /><br /></em></li>
</ul>
</div><!-- #faq -->
Upvotes: 0
Reputation: 37378
you need to search for each term individually, and build up a query.
var term = $(this).val().trim();
var words = term.split(' ');
var selector = "#result LI";
for(var i = 0; i < words.length; i++){
selector += ':contains(' + words[i] + ')'
}
$(selector).show();
Update: Here's a fiddle for you, with added case-insensitivity
Upvotes: 2