You are viewing an older version of Mapbox.js. Check out v3.0.1 for the latest.

Events methods

A set of methods shared between event-powered classes (like Map). Generally, events allow you to execute some function when something happens with an object (e.g. the user clicks on the map, causing the map 'fire' event).

Example

map.on('click', function(e) {
	alert(e.latlng);
});

Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:

function onClick(e) { ... }

map.on('click', onClick);
map.off('click', onClick);

Methods

Method Returns Description
addEventListener( <String> type, <Function> fn, <Object> context? ) this Adds a listener function (fn) to a particular event type of the object. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. 'click dblclick').
addOneTimeEventListener( <String> type, <Function> fn, <Object> context? ) this The same as above except the listener will only get fired once and then removed.
addEventListener( <Object> eventMap, <Object> context? ) this Adds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove}
removeEventListener( <String> type, <Function> fn?, <Object> context? ) this Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to addEventListener, you must pass the same context to removeEventListener in order to remove the listener.
removeEventListener( <Object> eventMap, <Object> context? ) this Removes a set of type/listener pairs.
removeEventListener() this Removes all listeners. An alias to clearAllEventListeners when you use it without arguments.
hasEventListeners( <String> type ) Boolean Returns true if a particular event type has some listeners attached to it.
fireEvent( <String> type, <Object> data? ) this Fires an event of the specified type. You can optionally provide an data object — the first argument of the listener function will contain its properties.
clearAllEventListeners() this Removes all listeners to all events on the object.
on( … ) this Alias to addEventListener.
once( … ) this Alias to addOneTimeEventListener.
off( … ) this Alias to removeEventListener.
fire( … ) this Alias to fireEvent.