Backbone.JS on() Event

The Backbone.JS event on() method binds an event to an object and executes the callback when an event is fired.

Syntax:

object.on(event, callback, context)    

Parameters:
event: This parameter is used to specify the event whose binding with an object is needed.
callback: This parameter is used to specify the callback function that will be executed when an event is fired.
context: This is an optional parameter which is used to specify an object to be used to pass to a callback function.

Example:

 

  
Example  
  
  
  
  
  
  
  
  

Output:

Triggered message: Hello World

Explanation:
The on() method will bind the event to an object. The function will then be executed when triggered and thus the message will be displayed.