The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

JQUERY - DOM MANIPULATION


JQuery provides methods to manipulate DOM in efficient way. You do not need to write big code to modify the value of any element's attribute or to extract HTML code from a paragraph or division.

JQuery provides methods such as .attr(), .html(), and .val() which act as getters, retrieving information from DOM elements for later use.

Content Manipulation

The html( ) method gets the html contents (innerHTML) of the first matched element. Here is the syntax for the method:

selector.html( )

Example

Following is an example which makes use of .html() and .text(val) methods. Here .html() retrieves HTML content from the object and then .text( val ) method sets value of the object using passed parameter:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = $(this).html();
$("#result").text( content );
});
});
</script>
<style>
#division{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on the square below:</p>
<span id="result"> </span>
<div id="division" style="background-color:blue;">
This is Blue Square!!
</div>
</body>
</html>

Click on the square below:

This is Blue Square!!

DOM Element Replacement

You can replace a complete DOM element with the specified HTML or DOM elements. The replaceWith( content ) method serves this purpose very well.

Here is the syntax for the method:

selector.replaceWith( content )

Here content is what you want to have instead of original element. This could be HTML or simple text.

Example

Following is an example which would replace division element with "<h1>JQuery is Great</h1>":

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).replaceWith("<h1>JQuery is Great</h1>");
});
});
</script>
<style>
#division{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on the square below:</p>
<span id="result"> </span>
<div id="division" style="background-color:blue;">
This is Blue Square!!
</div>
</body>
</html>

Click on the square below:

This is Blue Square!!

Removing DOM Elements

There may be a situation when you would like to remove one or more DOM elements from the document. JQuery provides two methods to handle the situation. The empty( ) method remove all child nodes from the set of matched elements whereas the remove( expr ) method removes all matched elements from the DOM. Here is the syntax for the method:

selector.remove( [ expr ])

or

selector.empty( )

You can pass optional paramter expr to filter the set of elements to be removed.

Example

Following is an example where elements are being removed as soon as they are clicked:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).remove( );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below:</p>
<span id="result"> </span>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

Click on any square below:


Inserting DOM Elements

There may be a situation when you would like to insert new one or more DOM elements in your existing document. JQuery provides various methods to insert elements at various locations. The after( content ) method insert content after each of the matched elements where as the before( content ) method inserts content before each of the matched elements. Here is the syntax for the method:

selector.after( content )

or

selector.before( content )

Here content is what you want to insert. This could be HTML or simple text.

Example

Following is an example where <div> elements are being inserted just before the clicked element:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).before('<div class="div"></div>' );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below:</p>
<span id="result"> </span>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

Click on any square below:


DOM Manipulation Methods

Following table lists down all the methods which you can use to manipulate DOM elements:
S.NoMethodDescription
1after( content )Insert content after each of the matched elements.
2append( content )Append content to the inside of every matched element.
3appendTo( selector )Append all of the matched elements to another, specified, set of elements.
4before( content )Insert content before each of the matched elements.
5clone( bool )Clone matched DOM Elements, and all their event handlers, and select the clones.
6clone( )Clone matched DOM Elements and select the clones.
7empty( )Remove all child nodes from the set of matched elements.
8html( val )Set the html contents of every matched element.
9html( )Get the html contents (innerHTML) of the first matched element.
10insertAfter( selector )Insert all of the matched elements after another, specified, set of elements.
11insertBefore( selector )Insert all of the matched elements before another, specified, set of elements.
12prepend( content )Prepend content to the inside of every matched element.
13prependTo( selector )Prepend all of the matched elements to another, specified, set of elements.
14remove( expr )Removes all matched elements from the DOM.
15replaceAll( selector )Replaces the elements matched by the specified selector with the matched elements.
16replaceWith( content )Replaces all matched elements with the specified HTML or DOM elements.
17text( val )Set the text contents of all matched elements.
18text( )Get the combined text contents of all matched elements.
19wrap( elem )Wrap each matched element with the specified element.
20wrap( html )Wrap each matched element with the specified HTML content.
21wrapAll( elem )Wrap all the elements in the matched set into a single wrapper element.
22wrapAll( html )Wrap all the elements in the matched set into a single wrapper element.
23wrapInner( elem )Wrap the inner child contents of each matched element (including text nodes) with a DOM element.
24wrapInner( html )Wrap the inner child contents of each matched element (including text nodes) with an HTML structure.
after( content ) Method

The after( content ) method inserts content after each of the matched elements.

Here is the simple syntax to use this method:

selector.after( content )

Parameters

Here is the description of all the parameters used by this method:
  • content: Content to insert after each target. This could be HTML or Text content
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).after('<div class="div"></div>' );
});
});;
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

Click on any square below to see the result:


append( content ) Method

The append( content ) method appends content to the inside of every matched element.

Syntax

Here is the simple syntax to use this method

selector.append( content )

Parameters

Here is the description of all the parameters used by this method:
  • content: Content to insert after each target. This could be HTML or Text content
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).append('<div class="div"></div>' );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

Click on any square below to see the result:


appendTo( selector ) Method

The appendTo( selector ) method appends all of the matched elements to another, specified, set of elements.

Syntax

Here is the simple syntax to use this method:

selector.appendTo( selector )

Parameters

Here is the description of all the parameters used by this method:
  • selector: This is the target to which the content will be appended.
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).appendTo("#result");
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<p id="result"> THIS IS TEST </p>
<hr />
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST



before( content ) Method


The before( content ) method inserts content before each of the matched elements.

Syntax

Here is the simple syntax to use this method:

selector.before( content )

Parameters

Here is the description of all the parameters used by this method:
  • content: Content to insert before each target. This could be HTML or Text content
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).before('<div class="div"></div>' );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


clone( bool ) Method

The clone( bool ) method clones matched DOM Elements, and all their event handlers, and select the clones.

This is useful for moving copies of the elements, and their events, to another location in the DOM.

Syntax

Here is the simple syntax to use this method:

selector.clone( bool )

Parameters

Here is the description of all the parameters used by this method:
  • bool: Set to true to enable cloning of event handlers.
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).clone().insertAfter(this);
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


clone( ) Method

The clone( ) method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

Syntax

Here is the simple syntax to use this method:

selector.clone( )

Parameters

Here is the description of all the parameters used by this method:
  • NA:
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).clone().insertAfter(this);
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


empty( ) Method

The empty( ) method removes all child nodes from the set of matched elements.

Syntax

Here is the simple syntax to use this method:

selector.empty( )

Parameters

Here is the description of all the parameters used by this method:
  • NA:
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).empty();
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

ONE
TWO
THREE

html( val ) Method

The html( val ) method sets the html contents of every matched element. This property is not available on XML documents.

Syntax

Here is the simple syntax to use this method:

selector.html( val )

Parameters

Here is the description of all the parameters used by this method:
  • val: This is the html content to be set.
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).html( "<h1>Click another square</h1>");
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


html( ) Method

The html( val ) method gets the html contents (innerHTML) of the first matched element. This property is not available on XML documents.

Syntax

Here is the simple syntax to use this method:

selector.html( )

Parameters

Here is the description of all the parameters used by this method:
  • NA:
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = $(this).html();
$("#result").html(content);
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<p id="result"> THIS IS TEST </p>
<div class="div" style="background-color:blue;">
<h1>This is square one </h1>
</div>
<div class="div" style="background-color:green;">
<h1>This is square two </h1>
</div>
<div class="div" style="background-color:red;">
<h1>This is square three </h1>
</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST

This is square one

This is square two

This is square three


insertAfter( selector ) Method

The insertAfter( selector ) method inserts all of the matched elements after another, specified, set of elements.

Syntax

Here is the simple syntax to use this method:

selector.insertAfter( selector )

Parameters

Here is the description of all the parameters used by this method:
  • selector: Content after which the selected element(s) is inserted.
Example

Following is a simple example showing the usage of this method. This inserts division element with ID of "source" after an element which is being clicked.

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$("#source").insertAfter(this);
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="source"></div>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


insertBefore( selector ) Method

The insertBefore( selector ) method inserts all of the matched elements before another, specified, set of elements.

Syntax

Here is the simple syntax to use this method:

selector.insertBefore( selector )

Parameters

Here is the description of all the parameters used by this method:
  • selector: Content before which the selected element(s) is inserted.
Example

Following is a simple example showing the usage of this method. This inserts division element with ID of "source" before an element which is being clicked.

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$("#source").insertBefore(this);
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
<div class="div" id="source"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


prepend( content ) Method

The prepend( content ) method prepends content to the inside of every matched element. Compare it with append( content ) method.

Syntax

Here is the simple syntax to use this method:

selector.prepend( content )

Parameters

Here is the description of all the parameters used by this method:
  • content: Content to insert after each target. This could be HTML or Text content
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).prepend('<div class="div"></div>' );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


prependTo( selector ) Method

The prependTo( selector ) method prepends all of the matched elements to another, specified, set of elements. Compare it with appendTo( selector ) method.

Syntax

Here is he simple syntax to use this method:

selector.prependTo( selector )

Parameters

Here is the description of all the parameters used by this method:
  • selector: This is the target to which the content will be prepended.
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).prependTo("#result");
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<p id="result"> THIS IS TEST </p>
<hr />
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST



remove( expr ) Method

The remove( expr ) method removes all matched elements from the DOM. This does NOT remove them from the jQuery object, allowing you to use the matched elements further. Compare it with empty() method.

Syntax

Here is the simple syntax to use this method:

selector.remove( expr )

Parameters

Here is the description of all the parameters used by this method:
  • expr: This is an optional jQuery expression to filter the set of elements to be removed
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).remove().appendTo("#result");
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<p id="result"> THIS IS TEST </p>
<hr />
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST


ONE
TWO
THREE

replaceAll( selector ) Method

The replaceAll( selector ) method replaces the elements matched by the specified selector with the matched elements.

Syntax

Here is the simple syntax to use this method:

selector.replaceAll( selector )

Parameters

Here is the description of all the parameters used by this method:
  • selector: The elements to find and replace the matched elements with.
Example

Following is a simple example showing the usage of this method. This replaces clicked one square by a new division:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$('<div class="div"></div>').replaceAll( this );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

ONE
TWO
THREE

replaceWith( content ) Method

The replaceWith( content ) method replaces all matched elements with the specified HTML or DOM elements. This returns the JQuery element that was just replaced, which has been removed from the DOM.

Syntax

Here is the simple syntax to use this method:

selector.replaceWith( content )

Parameters

Here is the description of all the parameters used by this method:
  • content : Content to replace the matched elements with.
Example

Following is a simple example showing the usage of this method. This replaces clicked one square by a new division. Compare the syntax with replaceAll( selector )method

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() { $("div").click(function () { $(this).replaceWith( ('<div class="div"></div>') ); }); }); </script> <style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

ONE
TWO
THREE

text( val ) Method

The text( val ) method gets the combined text contents of all matched elements. Similar to html( val ), but escapes HTML (replace "<" and ">" with their HTML entities).

Syntax

Here is the simple syntax to use this method:

selector.text( val )

Parameters

Here is the description of all the parameters used by this method:
  • val: This is the text value to be set.
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).text( "<h1>Click another square</h1>");
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" style="background-color:blue;"></div>
<div class="div" style="background-color:green;"></div>
<div class="div" style="background-color:red;"></div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:


text( ) Method

The html( val ) method gets the combined text contents of all matched elements.

The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents.

Syntax

Here is the simple syntax to use this method:

selector.text( )

Parameters

Here is the description of all the parameters used by this method:
  • NA
Example

Following is a simple example showing the usage of this method:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = $(this).text();
$("#result").text(content);
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<p id="result"> THIS IS TEST </p>
<div class="div" style="background-color:blue;">
<h1>This is square one </h1>
</div>
<div class="div" style="background-color:green;">
<h1>This is square two </h1>
</div>
<div class="div" style="background-color:red;">
<h1>This is square three </h1>
</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST

This is square one

This is square two

This is square three


wrap( elem ) Method

The wrap( elem ) method wraps each matched element with the specified element.

Syntax

Here is the simple syntax to use this method:

selector.wrap( elem )

Parameters

Here is the description of all the parameters used by this method:
  • elem : A DOM element that will be wrapped around each target.
Example

Following is a simple example showing the usage of this method. This wraps destination square with a square when any square gets cli cked:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = 'div class="div"';
$("#destination").wrap(document.createElement(content));
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">THIS IS TEST</div>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div> </body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST
ONE
TWO
THREE

wrap( html ) Method

The wrap( html ) method wraps each matched element with the specified HTML content.

This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document.

Syntax

Here is the simple syntax to use this method:

selector.wrap( html )

Parameters

Here is the description of all the parameters used by this method:
  • elem : A string of HTML that will be created on the fly and wrapped around each target.
Example

Following is a simple example showing the usage of this method. This wraps destination square with a sqaure when any square gets clicked:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = '<div class="div"></div>';
$("#destination").wrap( content );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">THIS IS TEST</div>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST
ONE
TWO
THREE

wrapAll( elem ) Method

The wrapAll( elem ) method wraps all the elements in the matched set into a single wrapper element.

Here is the simple syntax to use this method:

selector.wrapAll( elem )

Parameters

Here is the description of all the parameters used by this method:
  • elem : A DOM element that will be wrapped around the target.
Example

Following is a simple example showing the usage of this method. This wraps all the squares with a new square:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = "<div class='div'>";
$("div").wrapAll( document.createElement(content) );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">THIS IS TEST</div>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:
Element-1
Element-2
Element-3

ONE
TWO
THREE

wrapAll( html ) Method

The wrapAll( html ) method wraps all the elements in the matched set into a single wrapper element.

Here is the simple syntax to use this method:

selector.wrapAll( html )

Parameters

Here is the description of all the parameters used by this method:
  • html: A string of HTML that will be created on the fly and wrapped around each target.
Example

Following is a simple example showing the usage of this method. This wraps all the squares with a new square:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = "<div class='div'></div>";
$("div").wrapAll( content );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">THIS IS TEST</div>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST
ONE
TWO
THREE
wrapInner( elem ) Method

The wrapInner( elem ) method wraps the inner child contents of each matched element (including text nodes) with a DOM element.

Here is the simple syntax to use this method:

selector.wrapInner( elem )

Parameters

Here is the description of all the parameters used by this method:
  • html : A DOM element that will be wrapped around the target.
Example

Following is a simple example showing the usage of this method. This selects all divisions and wraps a bold tag around the content of clicked one square:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
$(this).wrapInner(document.createElement( "b" ));
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">THIS IS TEST</div>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST
ONE
TWO
THREE

wrapInner( html ) Method

The wrapInner( html ) method wraps the inner child contents of each matched element (including text nodes) with an HTML structure.

Here is the simple syntax to use this method:

selector.wrapInner( html )

Parameters

Here is the description of all the parameters used by this method:
  • html : A string of HTML that will be created on the fly and wrapped around the target.
Example

Following is a simple example showing the usage of this method. This selects all divisions and wraps a bold tag around the content of clicked one square:

<html>
<head>
<title>the title</title>
<script type="text/javascript"
src="/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div").click(function () {
var content = "<b></b>";
$(this).wrapInner( content );
});
});
</script>
<style>
.div{ margin:10px;padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class="div" id="destination">THIS IS TEST</div>
<div class="div" style="background-color:blue;">ONE</div>
<div class="div" style="background-color:green;">TWO</div>
<div class="div" style="background-color:red;">THREE</div>
</body>
</html>

This will produce the following result:

Click on any square below to see the result:

THIS IS TEST
ONE
TWO
THREE
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com