jQuery and Function Chaining

CK CK 3788 
Created at
Updated at  
541 0 0

Function chaining is a way to make code more readable and concise by calling multiple functions in sequence, where the output of one function becomes the input of the next. 
Imagine you have a series of tasks to complete, like washing your clothes:
1. Gather clothes: This is your first function.
2. Wash clothes: This function takes the output (clothes) from the first function as its input.
3. Dry clothes: This function takes the output (washed clothes) from the second function.

 

In function chaining, you can express this entire process in a single line of code:

gatherClothes()
.washClothes()
.dryClothes();


Instead of writing separate lines for each function, chaining allows you to link them together in a streamlined way. This improves readability and reduces the need for temporary variables. 
Key points about function chaining:

  • Each function in the chain should return a value.
  • The returned value becomes the input for the next function.
  • The functions in the chain are typically related in some way.

Example:

const number = 10;
const result = number
.toString() // Convert to string
.toUpperCase() // Convert to uppercase
.replace("10", "Ten"); // Replace "10" with "Ten"
console.log(result); // Outputs “TEN”

 

Here, the `toString` function returns a string, which is used as input by the `toUpperCase` function. The result of `toUpperCase` is then used as input for the `replace` function. This way, we achieve the desired result with a single chain of functions.

 

Function chaining in jQuery allows you to execute multiple actions on the same set of elements in a single line of code. This is accomplished by returning the jQuery object after each function call, enabling you to chain subsequent methods onto the same selection. It streamlines code, improves readability, and enhances performance by reducing the number of DOM traversals. For example, you can chain `$('.element').addClass('newClass').css('color', 'red').hide()` to add a class, change the color, and hide elements with the "element" class, all in one concise line.

I am learning new things about jQuery: function chaning is a really cool feature:
 

<script type="text/javascript">
$(document).ready(function() {
    $('#testclick').click(function() {   
         alert('Hello world clicker!');   
     });   
    $('#testclick').click(function() {   
        $('#div1').html( 'clicked' );
     });   
     
});
</script>

 

Tags Function Chaining jQuery toString toUpperCase Facebook X
Comments 0
Similar posts
  1. Simple asynchronous/synchronous $.ajax() example using jQuery to make an HTTP GET request
    861
  1. How To Close A Pop-up Window ?
    449
  2. Pop Up Window On Top
    523
  3. How to display message on browser status bar ?
    482
  4. New Window
    613
  5. Firefox Web Tweaking
    381
  6. Color Table
    371
  7. Cursor Changer
    387
  8. Dual Scroller
    335
  9. Enlarge Image
    517
  10. Confirm The Link Clicked
    632
  11. Send An Email Using A Bean
    1,020
  12. Select And Deselect All Checkboxes By Javascript
    634
  13. Javascript Mouse Event Handler Example
    404
  14. Getting WebSearchers On Your Site
    378
  15. Hiding HTML on the browser
    468
  16. How to get date time in Javascript ?
    502
  17. Javascript based Alarm Clock
    679
  18. Javascript implemented Calendar
    425
  19. Basic Date Display
    518
  20. Address Book
    581
  21. Displays the number of pages that the users browser has displayed in its session
    670
  22. Greeting For The Time Of Day
    395
  23. Access Granted
    593
  24. Cookie enabled browser checking in Javascript
    344
  25. Cookie Redirect
    505
  26. New content notifier in Javascript
    606
  27. Let popup once for event dialog or something like that in Javascript
    428
  28. Save And Restore Form Cookies in Javascript
    340
  29. How to record visitor's name by Cookie
    414
  30. Javascript & Cookie based Visitor Application
    440
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    29
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  4. How do I create animated background for Google Meet?
    22,910
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  8. Array of arguments passed to script in PHP
    97
  9. Invalid Auction Storms in Ad Tech
    106
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  11. The Two Clocks of Ad Server Architecture
    77
  12. Frequency Cap Architecture Overview
    526
  13. Collecting Proof-of-Play from 100,000 Screens
    152
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  15. What is RedisVL?
    114
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  17. TPU & GPU Cluster Management Tools
    257
  18. The Concept of Scaffolding in AI Agents
    244
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  23. Machine Learning: Training vs. Ranking
    229
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    672
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    200
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232