Assuming i have:
Mary
John, Mary, Dave
John, Dave, Mary
How about
$('li:contains("John"),li:contains("Mary")')
The correct syntax would be $("li:contains('John'),li:contains('Mary')").css("color","red")
But I found out that if you had many cases to test, jQuery will perform very badly (especially on IE6, I know, it's old but still in use). So I decided to write my own attribute filter.
This is how to use it: $("li:mcontains('John','Mary')").css("color","red")
jQuery.expr[':'].mcontains = function(obj, index, meta, stack){
result = false;
theList = meta[3].split("','");
var contents = (obj.textContent || obj.innerText || jQuery(obj).text() || '')
for (x=0;x<theList.length;x++) {
if (contents.indexOf(theList[x]) >= 0) {
return true;
}
}
return false;
};
It's easy:
$("li:contains('John'):contains('Mary')")
To find li
's that have text containing BOTH Mary AND John:
$('li:contains("Mary"):contains("John")')
To find li
's that have text containing EITHER Mary OR John:
$('li:contains("Mary"), li:contains("John")')
Just think of the :contains
as if it was a class declaration, like .class
:
$('li.one.two'). // Find <li>'s with classes of BOTH one AND two
$('li.one, li.two'). // Find <li>'s with a class of EITHER one OR two
It's the same with :contains
:
$('li:contains("Mary"):contains("John")'). // Both Mary AND John
$('li:contains("Mary"), li:contains("John")'). // Either Mary OR John
http://jsbin.com/ejuzi/edit