Javatpoint Logo
Javatpoint Logo

jQuery contents() method

The contents() method in jQuery is used to return the direct children, including the comment nodes and text of the matched element. The contents() method works similar to the jQuery children() method, except that the content() method also returns the comment nodes and text.

We can also use the contents() method to get the content document of an iframe if it is in the same domain as the main page.

Syntax

This method does not accept any arguments.

Let's see an illustration to understand the use of the contents() method.

Example

It is a simple example of using the contents() method. In this example, we are using the contents() method to get the content of the div element. The contents() method will return the content of the elements inside the div element. On clicking the given button, an alert box will be displayed showing the content of the elements of the div element.

To get the content of the specific child of the div element, we can use the filter() method to filter the children.

Test it Now

Output

After the execution, we will get the following output -

jQuery contents() method

After clicking the given button, the output will be -

jQuery contents() method




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA