jQuery - contents( ) Method


Advertisements

Description

The contents( ) method finds all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.

Syntax

Here is the simple syntax to use this method −

selector.contents( )

Parameters

Here is the description of all the parameters used by this method −

  • NA

Example

Consider you have an html file index.htm which we would use in an iframe.

Try the following example which shows how you can access the objects in an iframe from a parent window. This operation has become possible just because of contents() method.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script>
         $(document).ready(function(){
            var $content = $("iframe").contents();
            $content.find("body").append("I'm in an iframe!");
         });
      </script>
   </head>
	
   <body>
      <iframe src = "/jquery/index.htm" width = "300" height = "100"></iframe>
   </body>
</html>

This will produce following result −

jquery-traversing.htm

Useful Video Courses


Video

JQuery Online Training

27 Lectures 1 hours

Mahesh Kumar

Video

JQuery for Beginners

27 Lectures 1.5 hours

Pratik Singh

Video

jQuery Masterclass Course: JavaScript and AJAX Coding Bible

72 Lectures 4.5 hours

Frahaan Hussain

Video

Projects In JavaScript & JQuery

60 Lectures 9 hours

Eduonix Learning Solutions

Video

jQuery for Beginner to Advanced: 12 Projects included

17 Lectures 2 hours

Sandip Bhattacharya

Video

jQuery Memory Game Project - Fun coding Project with jQuery

12 Lectures 53 mins

Laurence Svekis

Advertisements