w3resource

jQuery: Add a callback or a collection of callbacks to a callback list

jQuery Fundamental - I : Exercise-42

Using jQuery add a callback or a collection of callbacks to a callback list.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Using jQuery add new callbacks to a callback list.</title>
</head>
<body>
</body>
</html>

JavaScript Code :

var f1 = function( value ) {
  console.log( "f1: " + value );
};

var f2 = function( value ) {
  console.log( "f2: " + value );
};
 
var callbacks = $.Callbacks();
 
// Add the function "f1" to the list
callbacks.add(f1);
 
// Fire the above items
callbacks.fire( "jQuery" );

 
// Add the function f2 to the list
callbacks.add(f2 );
 
// Fire the above items  again
callbacks.fire( "Javascript" );
 
// Outputs:
// "foo: world"
// "bar: world"

See the Pen jquery-fundamental-exercise-42 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Find all button inputs and mark them using jQuery.
Next: Disable a callback list from doing anything more.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.