Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is contents() method in jquery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 491
    Comment on it

    Hello Readers,

    .contents() method is the jquery method which is used to get the child or children of each element in the set of matched elements including text and comment nodes and its return value is the new jquery objects.

    .contents() method is same as the childen method in jquery but the difference is that children() method contain only text and comments nodes while the contents() method contain text, comment and HTML elements in the resulting jquery objects.

    Syntax :

    $(selector).contents()
    

    contents() method does not contain any parameter.

    Code Example :

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            $("div").contents().filter("em").wrap("<b/>");
        });
    });
    </script>
    </head>
    <body>
    
    <div><em>Hello world! What a beautiful day!</em></div>
    
    <p>paragraph/p>
    
    <button>Find all text nodes in div and wrap them</button><br>
    
    </body>
    </html>
    

    In the above code example by clicking on the button we search for all the text nodes inside the div elements and after that wrap them with using b bold element as a parameter pass inside wrap method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: