jQuery .contents() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Child nodes

What You’ll Learn

The .contents() traversing method collects all direct child nodes of each matched element — elements, text, and comments — one level down the DOM tree. This tutorial follows the official jQuery text-wrapping demos, paragraph conversion pattern, iframe document access, and compares .contents() with .children().

01

Syntax

.contents()

02

One level

Direct only

03

Text nodes

Included

04

nodeType

Filter nodes

05

Iframes

Same origin

06

Since 1.2

Core API

Introduction

A <div> might hold a paragraph element, raw text, line breaks, and even HTML comments — all as separate DOM nodes. When you call .children(), jQuery returns only the element tags and skips the text in between. To work with every direct child node, including text and comments, use .contents().

Available since jQuery 1.2, .contents() takes no arguments and travels exactly one level down — the same depth as .children() — but returns a richer set of nodes. It is especially useful for wrapping loose text, cleaning up mixed content, or reaching inside a same-origin iframe.

Understanding the .contents() Method

Given a jQuery object representing one or more DOM elements, .contents() reads the childNodes of each element and returns them as a new jQuery collection. Element nodes, text nodes, and comment nodes are all included. Since jQuery 3.2, contents of <template> elements are returned as well.

Important caveat from the jQuery docs: most jQuery methods do not support text or comment nodes — only a few (like .wrap(), .replaceWith(), and .filter()) work on them. Always filter by nodeType before chaining operations that expect elements.

💡
Beginner Tip

nodeType === 1 means element, nodeType === 3 means text, and nodeType === 8 means comment. Filter after .contents() to target the node kind you need.

📝 Syntax

General form of .contents:

jQuery
.contents()

Parameters

  • None — .contents() does not accept any arguments.

Return value

  • A new jQuery object containing all direct child nodes (elements, text, comments) of each matched element.

Official jQuery API paragraph conversion pattern

jQuery
$( ".container" )
  .contents()
  .filter( function () { return this.nodeType === 3; } )
  .wrap( "<p>" )
  .end()
  .filter( "br" )
  .remove();

⚡ Quick Reference

GoalCode
All direct child nodes$("div").contents()
Text nodes only$("p").contents().filter(function(){ return this.nodeType === 3; })
Non-element nodes$("p").contents().filter(function(){ return this.nodeType !== 1; })
Element children only$("div").children()
Iframe document$("#frame").contents()
Links inside iframe$("#frame").contents().find("a")

📋 .contents() vs .children() vs .find()

Three downward traversal methods — different node types and depth.

.contents()
1 level

Elements + text + comments

.children()
1 level

Element nodes only

.find()
any depth

Descendant elements only

nodeType
1 / 3 / 8

Filter after .contents()

Node typeCodeExample
ElementnodeType === 1<span>, <br>
TextnodeType === 3"Hello John"
CommentnodeType === 8<!-- note -->

Examples Gallery

Examples 1–3 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for text node manipulation.

Example 1 — Official Demo: Wrap Text Nodes in <b>

Find non-element child nodes inside a paragraph and wrap them with bold tags — official Example 1.

jQuery
$( "p" )
  .contents()
  .filter( function () {
    return this.nodeType !== 1;
  } )
  .wrap( "<b>" );
Try It Yourself

How It Works

.contents() returns text nodes that .children() would miss. Filtering nodeType !== 1 keeps text and comments, excluding element tags. .wrap("<b>") wraps each text node individually.

Example 2 — Official Demo: Convert Line-Break Text into Paragraphs

Turn a blob of text separated by <br> tags into well-formed paragraphs — the main jQuery API documentation pattern.

jQuery
$( ".container" )
  .contents()
  .filter( function () { return this.nodeType === 3; } )
  .wrap( "<p>" )
  .end()
  .filter( "br" )
  .remove();
Try It Yourself

How It Works

Text nodes (nodeType === 3) get wrapped in paragraphs. After .end() returns to the full contents set, element br nodes are filtered and removed — cleaning up the old separators.

📈 Practical Patterns

Compare with .children(), inspect node types, and reach into iframes.

Example 3 — .contents() vs .children() on the Same Element

A div with mixed text and element children returns different counts depending on which method you use.

jQuery
var $box = $( "#mixed" );

console.log( "contents:", $box.contents().length ); // elements + text
console.log( "children:", $box.children().length ); // elements only
Try It Yourself

How It Works

Whitespace text nodes between elements count toward .contents() but are invisible to .children(). This is the core difference between the two one-level methods.

Example 4 — Inspect and Label Each Child Node Type

Loop through .contents() results and report whether each node is an element, text, or comment.

jQuery
var labels = { 1: "element", 3: "text", 8: "comment" };

$( "#inspect" ).contents().each( function () {
  var kind = labels[ this.nodeType ] || "other";
  console.log( kind + ":", this.nodeName || this.textContent.trim().slice( 0, 20 ) );
} );
Try It Yourself

How It Works

Inspecting nodeType and nodeName is the standard way to understand what .contents() returns before applying further jQuery methods.

Example 5 — Official Demo: Style Links Inside an Iframe

Access a same-origin iframe document with .contents() and style anchors inside — official Example 2.

jQuery
$( "#frameDemo" ).contents().find( "a" ).css( "background-color", "#BADA55" );
Try It Yourself

How It Works

On an iframe element, .contents() returns the iframe’s document object. Chain .find("a") to query inside it. This only works when the iframe is same-origin — cross-origin pages block script access.

🚀 Common Use Cases

  • Wrap loose text — bold or paragraph-wrap text nodes that sit beside elements.
  • Clean mixed markup — remove br separators after converting text blocks to paragraphs.
  • Whitespace inspection — detect unexpected text nodes causing layout gaps.
  • Same-origin iframe styling — theme or highlight links inside embedded documents.
  • Template contents — read child nodes of <template> elements (jQuery 3.2+).
  • Comment node removal — filter nodeType === 8 and strip development comments.

🧠 How .contents() Reads Child Nodes

1

Start with parents

Current jQuery set holds one or more container elements.

Input
2

Read childNodes

Every direct child — elements, text, comments — is collected.

All types
3

Filter by nodeType

Isolate text (3), elements (1), or comments (8) before chaining.

Filter
4

Transform or query

Chain .wrap(), .remove(), or .find() on iframe documents.

📝 Notes

  • Available since jQuery 1.2; template element support since jQuery 3.2.
  • Takes no arguments — unlike .children(selector).
  • Travels exactly one DOM level — same depth as .children().
  • Most jQuery methods do not work on text nodes — filter first, then chain carefully.
  • Iframe access requires same-origin policy compliance.
  • For element-only direct children, .children() is simpler and more predictable.

Browser Support

.contents() has been part of jQuery since 1.2+. It relies on standard DOM childNodes traversal and works wherever jQuery runs.

jQuery 1.2+

jQuery .contents()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Iframe document access follows same-origin browser rules.

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.contents() Universal

Bottom line: Safe in any jQuery project. Use .contents() for text nodes and iframe documents; use .children() for element-only queries.

Conclusion

The jQuery .contents() method selects all direct child nodes — elements, text, and comments — one level down from each matched parent. It is the right tool when raw text nodes matter or when you need to reach inside a same-origin iframe.

Remember the official paragraph conversion pattern: filter text nodes with nodeType === 3, wrap them, then clean up br elements. When you only need element tags, .children() is the simpler choice.

💡 Best Practices

✅ Do

  • Filter by nodeType before wrapping or styling text nodes
  • Use .contents() for iframe same-origin document access
  • Prefer .children() when text nodes are irrelevant
  • Check the jQuery docs before chaining methods on text nodes
  • Use .end() to return to the full contents set after filtering

❌ Don’t

  • Call .css() or .addClass() directly on text nodes
  • Expect .contents() to reach nested text inside child elements
  • Access cross-origin iframe documents — browsers block it
  • Confuse .contents() with .html() — different purpose
  • Assume whitespace text nodes are harmless — they can affect layout

Key Takeaways

Knowledge Unlocked

Five things to remember about .contents()

All direct child nodes, one level down.

5
Core concepts
T 02

Text nodes

Included

Type 3
1 03

One level

Direct only

Depth
🔍 04

nodeType

Filter first

Pattern
🔗 05

Iframe

Same origin

Extra

❓ Frequently Asked Questions

.contents() returns a new jQuery object containing all direct child nodes of each matched element — element nodes, text nodes, and comment nodes. It travels one DOM level down, like .children(), but includes non-element nodes that .children() skips.
.children() returns only direct child element nodes (tags like p, span, li). .contents() returns every direct child node — including raw text and comments. Both travel exactly one level down. Use .contents() when you need to read or manipulate text nodes; use .children() for element-only queries.
Most jQuery methods expect element nodes. Text nodes use nodeType 3, comment nodes use 8, and elements use 1. After .contents(), filter with .filter(function(){ return this.nodeType === 3; }) to isolate text, or nodeType !== 1 to exclude elements before wrapping or styling.
Yes, when the iframe is same-origin with the main page. $('#myFrame').contents() returns the iframe's document object, and you can chain .find('a') or other methods to work inside it. Cross-origin iframes block access for security.
No. Like .children(), .contents() only inspects direct child nodes one level down. Nested text or elements inside a child element are not included — use .find() for deeper searches on element nodes.
No by itself — it only queries child nodes. However, you often chain methods like .wrap(), .remove(), or .replaceWith() after filtering .contents() results, which do modify the DOM.
Did you know?

The jQuery API’s paragraph conversion recipe is a classic DOM-normalization technique: legacy CMS output often mixes raw text and <br> tags inside a single container. .contents() exposes those hidden text nodes so you can wrap them in semantic <p> tags — something .children() alone cannot do because it never sees the text.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful