Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
Chaining is a robust technique provided by jQuery which allows us to chain together actions or methods. With jQuery chaining, we can run multiple jQuery actions or methods on the same set of elements within a single line of the statement. This technique is really neat and saves developers from lengthy code structures. It also helps browsers speed up their performance by saving them from finding the same elements more than once.
Method chaining is possible in jQuery because most of the jQuery methods return a jQuery object which can further call another method. Let us understand this concept through some examples which implement this jQuery chaining of methods.
Example #1
Example illustrates the implementation of jQuery chaining by binding together four methods: css(), slideUp(), slideDown() and fade().
Code:
<!DOCTYPE html>
<html>
<head>
<title>Example for jQuery chaining</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function() {
$(“button”).click(function() {
$(“p”)
.css(“color”, “green”)
.slideUp(1000)
.slideDown(1000)
.fadeOut(1000);
</script>
<style>
p {
color: maroon;
font-weight: bolder;
font-size: large;
}
</style>
</head>
<body>
<p>jQuery Chaining of Methods</p>
<button>Click to see the effect</button>
</body>
</html>
The example illustrates the chaining of css() and animate() methods.
Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″ />
<title>Example of jQuery Method Chaining</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function() {
$(“.btn1”).click(function() {
$(“p”)
.css(“color”, “green”)
.animate({ width: “100%” })
.animate({ fontSize: “46px”
$(“.btn2”).click(function() {
$(“p”).removeAttr(“style”);
</script>
<style>
p {
color: maroon;
font-weight: bolder;
font-size: large;
}
div {
width: 450px;
height: 300px;
padding: 20px;
font-size: medium;
text-align: center;
margin: auto;
background-color: lightblue;
font-weight: bold;
border: 3px solid teal;
margin-top: 50px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div>
<p>jQuery Chaining</p>
<button type=”button” class=”btn1″>Click to see the effect</button>
<button type=”button” class=”btn2″>Reset</button>
</div>
</body>
</html>
An example where a jQuery method does not return a jQuery object.
Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″ />
<title>jQuery Chaining of Methods</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function() {
$(“button”).click(function() {
//This will show jQuery effect due to method chaining
$(“h2”)
.html(“jQuery Chaining”)
.addClass(“chain”)
.fadeOut(2000);
//This will not show any effect since no jQuery object has been returned.
$(“p”)
.html()
.addClass(“chain”);
</script>
<style>
p {
color: maroon;
font-weight: bolder;
font-size: large;
}
div {
width: 450px;
height: 300px;
padding: 20px;
font-size: medium;
text-align: center;
margin: auto;
background-color: lightblue;
font-weight: bold;
border: 3px solid teal;
margin-top: 50px;
margin-bottom: 10px;
}
.chain {
padding: 20px;
font-size: medium;
background-color: lightblue;
}
</style>
</head>
<body>
<div>
<h2>This is jQuery Method Chaining</h2>
<p>This paragraph will not show any effect</p>
<button type=”button”>Click Me</button>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Sliding effect in jQuery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function() {
$(“#btn”).click(function() {
$(“.d”).slideToggle(“slow”, function() {
$(“.message”).text(” Transition has Completed”);
</script>
<style>
.d {
background-color: lightpink; margin-top: 20px;
padding: 10px; text-align: center; font-size: 18px; border: solid 1px; height: 200px; width: 300px;
}
.message {
font-weight: bold; font-size: medium; color: maroon; margin-left: 10px;
}
</style>
</head>
<body>
<button id=”btn”
style=”background:lightseagreen;cursor: pointer;font-size: 18px;”>
</button>
<div class=”d”>
<br/><b>jQuery</b> <br /><br />Sliding Effect<br /><br />slideToggle()
</div>
<div class=”message”></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Sliding effect in jQuery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function() {
$(“#head”).click(function() {
$(“#content”).slideToggle(“slow”);
</script>
<style> #content, #head {
padding: 10px; margin: auto;
text-align: center; width: 500px;
text-align: center;
background-color: lightseagreen; border: solid 1px lightgray;
}
#content { display: none; height: 300px;
}
</style>
</head>
<body>
<div id=”content”>
jQuery Sliding Effect<br /><br />
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Sliding effect in jQuery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function() {
$(“#up”).click(function() {
$(“#content”).slideUp(“400”, function() {
$(“#down”).click(function() {
$(“#content”).slideDown(“fast”, function() {
$(“#toggle”).click(function() {
$(“#content”).slideToggle(“slow”, function() {
</script>
<style>
#content,
#up {
padding: 10px;
text-align: center;
width: 500px;
text-align: center;
background-color: lightseagreen;
border: solid 1px lightgray;
}
#down {
padding: 10px;
text-align: center;
width: 500px;
text-align: center;
background-color: lightseagreen;
border: solid 1px lightgray;
}
#toggle { padding: 10px;
text-align: center;
width: 500px;
text-align: center;
background-color: lightseagreen;
border: solid 1px lightgray;
}
#content {
display: none;
height: 300px;
}
.message {
font-weight: bold;
font-size: medium;
margin-top: 30px;
color: maroon;
margin-left: 10px;
}
</style>
</head>
<body>
<div id=”content”>
jQuery Sliding Effect<br /><br />
</div>
<div class=”message”></div>
</body>
</html>
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.