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:
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:
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.
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.
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
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>