Filtering by Content

jQuery's available content filters.

TypeDescription
:contains()Selects all elements that contain the specified text.
:emptySelect empty elements from the DOM.
:has()Select elements which contain at least one element that matches the specified selector.
:parentSelects all elements that are the parent of another element, including text nodes.

Using :contains() filter you can select an element that contains a matching text directly inside the element, inside of a descendant, or even a combination of both.

The following code sample illustrates a use of contains. It selects all paragraphs that contain the text string "test" and stores them in a variable test.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>
            $(function(){ 
                var test = $("p:contains('test')").length; 
                document.writeln(test); 
            });
        </script> 
    </head> 
    <body>
        <p>test </p> 
        <p>this is a test</p> 
        <p>another test</p> 
    </body> 
</html>
  
Click to view the demo

A related selector, :has(), returns elements that contain at least one element that matches the specified selector.

This allows you to select elements that have a descendent selector of a certain type amongst any of its children.

The following code sample illustrates using :has() to select a div that contains a paragraph as one of its many children out of all the divs on a page.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script> 
            $(function() { 
                var hasDemo = $("div:has('p')").attr("id"); 
                document.writeln(hasDemo); 
            }); 
        </script> 
    </head> 
    <body> 
        <div id="yay"> 
            <ul>
                <li> 
                    <p>A</p> 
                    <p>B</p> 
                    <p>C</p> 
                </li>
            </ul> 
        </div> 
        <div id="nay">
            <ul>
                <li>D</li> 
            </ul> 
        </div> 
    </body> 
</html>
  
Click to view the demo

If you want to select an element that doesn't contain anything, you can use the :empty() selector. This selector returns all elements that contain no children. This includes text.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>
            $(function(){ 
                var nothing = $("p:empty").length; 
                document.writeln(nothing) //returns 1
            });
        </script> 
    </head> 
    <body>
        <div> 
            <p></p> 
            <p>something here</p>
        </div> 
    </body> 
</html>
  
Click to view the demo

The opposite of :empty() is :parent(). It selects elements that have children.

The following code shows an example of using :parent() selecting a single div with children and logging the div's ID.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>
            $(function(){ 
                var parentDemo = $("div:parent"); 
                document.writeln(parentDemo.attr("id")) //returns proudParent
            });
        </script> 
    </head> 
    <body>
        <div id="empty"></div> 
        <div id="proudParent"> 
            <ul>
                <li> Children! </li> 
            </ul> 
        </div> 
    </body> 
</html>
  
Click to view the demo

It's important to note that :parent() will select elements that contain child text nodes. This can include text nodes with just whitespace text.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>
            $(function(){ 
                var parents = $("div:parent").length; 
                document.writeln(parents) //returns 2
            });
        </script> 
    </head> 
    <body> 
        <div id="empty"> 
        </div> 
        <div id="proudParent">
            <ul>
                <li> Children! </li> 
            </ul> 
        </div> 
    </body> 
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    jQuery  

Selector:
  1. jQuery Selector
  2. DOM Element Selection
  3. Filter Selectors
  4. Selecting by Position
  5. Filtering by Content
  6. Filtering by Relationships
  7. Escape characters
  8. ID attribute
  9. Tag Name
  10. Class Name
  11. Descendant
  12. Child (E > F)
  13. General sibling (E ~ F)
  14. Multiple expressions (E, F, G)
  15. Universal (*)
  16. Numbered child (:nth-child(n/even/odd/expr))
  17. First child (:first-child)
  18. Last child (:last-child)
  19. Only child (:only-child)
  20. Not (:not(E))
  21. Empty (:empty)
  22. Attribute selectors
  23. Attribute ([attr])
  24. Attribute equals ([foo=bar])
  25. Attribute does not equal ([foo!=bar])
  26. Attribute begins with ([foo^=bar])
  27. Attribute ends with ([foo$=bar])
  28. Attribute contains ([foo*=bar])
  29. Attribute contains word ([foo~=bar])
  30. Attribute contains prefix ([foo|=bar])
  31. Attribute exists $("[attributeName*='value']");
  32. Form selectors (:input)
  33. Form text fields (input:text)
  34. Form Password field (input:password)
  35. Form Radio button (input:radio)
  36. Form Checkbox (input:checkbox)
  37. Form Submit button (input:submit)
  38. Form Image button (input:image)
  39. Form Reset button (input:reset)
  40. Form button (input:button)
  41. Form File upload (input:file)
  42. Form Enabled form element (input:enabled)
  43. Form Disabled form element (input:disabled)
  44. Form Checked box (input:checked)
  45. Form Selected option (input:selected)
  46. Element at index (:eq(n))
  47. Greater than (:gt(n))
  48. Less than (:lt(n))
  49. First (:first)
  50. Last (:last)
  51. Even element (:even)
  52. Odd element (:odd)
  53. Is parent (:parent)
  54. Contains text (:contains(text))
  55. Contains element (:has(E))
  56. Visible (:visible)
  57. Hidden (:hidden)
  58. Header element (:header)
  59. Currently animating (:animated)
  60. $(this)
  61. Custom User Selectors