Filtering by Content
jQuery's available content filters.
Type | Description |
---|---|
:contains() | Selects all elements that contain the specified text. |
:empty | Select empty elements from the DOM. |
:has() | Select elements which contain at least one element that matches the specified selector. |
:parent | Selects 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>
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>
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>
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>
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>
JavaScript Book
jQuery
- jQuery Selector
- DOM Element Selection
- Filter Selectors
- Selecting by Position
- Filtering by Content
- Filtering by Relationships
- Escape characters
- ID attribute
- Tag Name
- Class Name
- Descendant
- Child (E > F)
- General sibling (E ~ F)
- Multiple expressions (E, F, G)
- Universal (*)
- Numbered child (:nth-child(n/even/odd/expr))
- First child (:first-child)
- Last child (:last-child)
- Only child (:only-child)
- Not (:not(E))
- Empty (:empty)
- Attribute selectors
- Attribute ([attr])
- Attribute equals ([foo=bar])
- Attribute does not equal ([foo!=bar])
- Attribute begins with ([foo^=bar])
- Attribute ends with ([foo$=bar])
- Attribute contains ([foo*=bar])
- Attribute contains word ([foo~=bar])
- Attribute contains prefix ([foo|=bar])
- Attribute exists $("[attributeName*='value']");
- Form selectors (:input)
- Form text fields (input:text)
- Form Password field (input:password)
- Form Radio button (input:radio)
- Form Checkbox (input:checkbox)
- Form Submit button (input:submit)
- Form Image button (input:image)
- Form Reset button (input:reset)
- Form button (input:button)
- Form File upload (input:file)
- Form Enabled form element (input:enabled)
- Form Disabled form element (input:disabled)
- Form Checked box (input:checked)
- Form Selected option (input:selected)
- Element at index (:eq(n))
- Greater than (:gt(n))
- Less than (:lt(n))
- First (:first)
- Last (:last)
- Even element (:even)
- Odd element (:odd)
- Is parent (:parent)
- Contains text (:contains(text))
- Contains element (:has(E))
- Visible (:visible)
- Hidden (:hidden)
- Header element (:header)
- Currently animating (:animated)
- $(this)
- Custom User Selectors