JavaScript jQuery search for paragraphs containing multiple words

I have an unordered list called test

<ul id='test'></ul>

it is dynamically populated with data via ajax. Each item 'li' is a div containing 'p' paragraphs. Each paragraph contains some information.

Ex:

<li> <div> <p> test </p> </div> </li>
<li> <div> <p> hi how is it going?</p> </div> </li>
<li> <div> <p> not a test</p> </div> </li> 
<li> <div> <p> whoa</p> </div> </li>

I also have a search box which i can get a search term from, I use:

var searchTerm = $("#search").val().trim().split(' '); // an array of words searched for

What I am trying to do is find a way to select all 'li' elements which contain all or some of the search words, but I'm not sure how to approach it best.

Currently, I am doing this:

var results = $('p:contains("'+ searchTerm[0] +'")');

to get an exact match on the first term, but I want to search for multiple terms, not just one.

I would want to search for 'test hi' and get back three nodes cause it searches for 'test' and 'hi'. I also thought of:

var results2 = $('p').filter(function( index ) {
    return ( this +':contains("'+ searchTerm +'")' );
});

Anyone point me in the right direction?

Answer:1

You could do some black magic with the selector, like this:

var results = $('p:contains("' + searchTerm.join('"), p:contains("') + '")');

This looks hard, but I'll explain it.

It joins the search terms with "), p:contains(". Then it just adds the missing p:contains(" and ") to the ends of the result string and searches for it.

Answer:2

I am trying to port a program from python (using OpenCV) to javascript/html, which generates a video where pixel colour are determined by a random function of its coordinates and its frame number. I ...

I am trying to port a program from python (using OpenCV) to javascript/html, which generates a video where pixel colour are determined by a random function of its coordinates and its frame number. I ...

I was having and bizarre error and I just want to understand why. I was getting a 'bad route' return from my css and Javascript files. As it turns all my paths were correct but the problem was caused ...

I was having and bizarre error and I just want to understand why. I was getting a 'bad route' return from my css and Javascript files. As it turns all my paths were correct but the problem was caused ...

In the following piece of code store.nextId and store.cache is used in the add method. I was wondering why not this? var store = { nextId: 1, cache: {}, add: function(fn) { if (!fn.id) { ...

In the following piece of code store.nextId and store.cache is used in the add method. I was wondering why not this? var store = { nextId: 1, cache: {}, add: function(fn) { if (!fn.id) { ...

Using jQuery I can create a real DOM node, but it is not located in the DOM. So how does jQuery do it? My guess that it might create it first in the DOM and then removes it. // here `el` is not a ...

Using jQuery I can create a real DOM node, but it is not located in the DOM. So how does jQuery do it? My guess that it might create it first in the DOM and then removes it. // here `el` is not a ...

  1. jquery on dynamically created elements
  2. dynamically created elements jquery event
  3. bind dynamically created elements jquery
  4. jquery selecting dynamically created elements
  5. jquery each dynamically created elements
  6. jquery handle dynamically created elements
  7. jquery count dynamically created elements
  8. create html elements in jquery
  9. jquery selecting created elements