Open In App

jQuery contents() Method

Last Updated : 07 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The contents() is an inbuilt method in jQuery that returns all the direct children, including text and comment nodes for the selected element.

Syntax:

$(selector).contents()

Parameter: It does not accept any parameter.

Return Value: It returns all the direct children elements of the selected element.

jQuery code to show the working of this method:

Example 1:

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
    <script>
        $(document).ready(function () {
            //jQuery code to perform this method
            $("button").click(function () {
                $("div").contents().filter("p").wrap("<b/>");
            });
        });
    </script>
    <style>
        #p1 {
            width: 420px;
            padding: 50px;
            display: block;
            border: 2px solid green;
            font-size: 30px;
        }
    </style>
</head>
 
<body>
    <div>
        <!-- This paragraph will get bold after click on
            the button -->
        <p id="p1">Welcome to GeeksforGeeks !!!</p>
    </div>
    <!-- click on this button -->
    <button>Click Me!</button>
    <br>
</body>
 
</html>


Output:

jquery-6

Example 2: In the below code, no need to click on any button.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
   </script>
    <style>
        #p1 {
            display: block;
            width: 400px;
            padding: 30px;
            border: 2px solid green;
            font-size: 30px;
        }
    </style>
</head>
 
<body>
    <!-- This paragraph will get bold -->
    <p id="p1">
        Welcome to GeeksforGeeks !
    </p>
    <script>
        $("p")
            .contents()
            .filter(function () {
                return this.nodeType !== 1;
            })
            .wrap("<b></b>");
    </script>
</body>
 
</html>


Output:



Previous Article
Next Article

Similar Reads

How to show contents of selected row of a table in Bootstrap model using jQuery ?
The task is to fetch data from a row of the table and show them on the bootstrap model. Bootstrap model: The Modal component is a kind of dialog box or a popup window that is displayed on top of the present page, on clicking the provided button on the page. However, the Model automatically closes on clicking the model's backdrop. Moreover, it shoul
4 min read
How to remove contents of elements using jQuery ?
In this article, we will discuss how to remove the contents of the elements using jQuery. To remove the contents of elements, we will use the empty() method and the remove() method. The jQuery empty() method is used to remove all child nodes and their content for the selected elements. This method does not accept any parameter. Syntax: $(selector).
3 min read
How to get text contents of all matched elements using jQuery ?
In this article, we will see how to get the text content of all matched class elements using jQuery. To get the text content, we use the text() method, which helps to set or return the text content of the element. Syntax: $('Selector').text();Approach 1: We create a div element that contains multiple div's with class "content", then we use the jQue
2 min read
ASP Contents.Remove Method
The ASP Contents.Remove Method is used to remove an item from the Application.Contents collection. It is a predefined method of the application type Object, Syntax: Application.Contents.Remove(name|index) Parameter Values: It contains the value i.e name which represents the index of the specified items that would be removed from the application. Re
1 min read
ASP Contents.RemoveAll Method
The ASP Contents.RemoveAll Method is used to remove or erase all the items from the content List. It is a predefined method of the Application type Object. Syntax: Application.Contents.RemoveAll() Parameter Value : This method does not contain any value. Return Value : It do not return any value. Example: Below code used to delete all the items fro
1 min read
ASP Session.Contents.RemoveAll Method
The Session.Contents.RemoveAll Method in ASP is used to remove or erase all the items from the content List. It is a predefined method of the Session type Object. Syntax: Session.Contents.RemoveAll() Parameter Value: This method does not contain any value. Return Value: It does not return any value. Example Code: Below code used to delete all the i
1 min read
ASP Session.Contents.Remove Method
The Session.Contents.Remove Method in ASP is used to remove an item from Session Contents collection. It is a predefined method of the Session type Object. Syntax Session.Contents.Remove(name|index) Parameter Values: It contains the value i.e name which represents the index of the specified items that would be removed from the Session. Example: Bel
1 min read
Copy the entire contents of a directory to another directory in PHP
Given a directory and the task is to copy the content of the directory to another directory using PHP functions. There are many functions used to copy the content of one directory to another. Used Functions: copy() Function: The copy() function is used to make a copy of a specified file. It makes a copy of the source file to the destination file an
3 min read
How to make div not larger than its contents using CSS?
There are three ways to achieve this problem: By default case Using inline-block property Using fit-content property in width and height Default Case: In HTML div is by default fit to content inside it. The example goes like this: Example 1: &lt;!DOCTYPE html&gt; &lt;html lang = &quot;en&quot; dir = &quot;ltr&quot;&gt; &lt;head&gt; &lt;meta charset
3 min read
How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
In PHP if you want to delete the file or directory then keep one thing in mind that you cannot delete the file or directory directly there is a condition on that i.e. there are some security issues are there so the best way to do this is you first have to delete the data present in the file or the sub files or directories present in it . Then only
3 min read