jQuery .contents() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.contents() (since jQuery 1.2) returns the direct child nodes of each matched element — including text and comment nodes. It is like .children(), but broader. Since 3.2 it also returns contents of <template> elements.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("p").contents();                    // elements + text + comments
$("p").children();                    // elements only

// Official pattern — wrap non-element nodes
$("p").contents()
  .filter(function () { return this.nodeType !== 1; })
  .wrap("<b>");

// Same-origin iframe document
$("#frame").contents().find("a");
CallWhat it does
.contents()Learn jQuery .contents(): get direct child nodes including text and comments, filter by nodeType, wrap text nodes, compare with .children(), and access same-origin iframe documents.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the parent(s) — e.g. $("p") or $(".container").
Step 2Call .contents() (no arguments) for every direct child node.
Step 3Filter when needed: nodeType === 3 for text, !== 1 for non-elements.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Official Wrap Text in <b>

Wrap non-element child nodes inside a paragraph (official demo idea).

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

How It Works

  1. Contents. Collect every direct child node of p.
  2. Filter. Keep nodes that are not elements (nodeType !== 1).
  3. Wrap. Each remaining text node gets a <b> parent.

Example 2 — Text Chunks to Paragraphs

Official idea: wrap text nodes in <p> and remove <br> separators.

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

How It Works

  1. Contents. Read every direct child of the container.
  2. Text only. nodeType === 3 keeps text chunks.
  3. Wrap. Each chunk becomes a paragraph.

Example 3 — .contents() vs .children()

Same parent — compare how many nodes each method returns.

jQuery
var $box = $("#mixed");
$("#out").text(
  "contents: " + $box.contents().length +
  "\nchildren: " + $box.children().length
);
Try It Yourself

How It Works

  1. Markup. Text, a span, more text, a p, more text.
  2. contents. Counts every direct node (text + elements).
  3. children. Counts only the span and p.

Frequently Asked Questions

.contents() returns a new jQuery object with all direct child nodes of each matched element — elements, text nodes, and comments. Like .children(), it goes one level down, but it also includes non-element nodes.
.children() returns only direct child elements. .contents() also includes text and comment nodes. Use .contents() when you need text nodes; use .children() for element-only queries.
Most jQuery methods expect element nodes. Text nodes use nodeType 3, comments use 8, elements use 1. Filter after .contents() before wrapping or styling text.
Yes, when the iframe is same-origin. $('#frame').contents() returns the iframe document so you can chain .find(). Cross-origin iframes are blocked for security.
No. It only inspects direct child nodes one level down. Nested text inside a child element is not included — go into that child first, or use .find() for element descendants.
No. .contents() accepts no parameters. Filter afterward with .filter() if you need only text nodes or only elements.

Next: .find() Method

Iterate over a jQuery set with a callback for each element.

.find() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful