Can I get the current layer in control's event?

    if (obj.overlay) {
        input = document.createElement('input');
        input.type = 'checkbox';
        input.defaultChecked = checked;
    } else {
        input = this._createRadioElement('leaflet', checked);
L.DomEvent.on(input, 'click', this._onInputClick, this);

    var id =e. currentTarget;


When user click the checkbox, it will trigger _onInputClick event. This control will be created under a leaflet layer. I want to get the current layer. For example, I can get the current control like e.currentTarget. Is it possible to get the current layer by Leaflet library?

  • As your questions are very programming-oriented, you may consider switching to Stack Overflow, even though posting these questions here is also legitimate. Please provide more context about the objects in your code, what you try to achieve and why, or you may end up with many unanswered posts. E.g. in this case, what is "input", what is "control"?
    – ghybs
    Commented Nov 30, 2015 at 4:53
  • ghybs thank you for your correct. I will try to learn how to ask a question.
    – Hu Kenneth
    Commented Nov 30, 2015 at 5:14

2 Answers 2


I guess you are building a Leaflet Control based on L.Control.Layers?

In that case, you can simply continue using a similar scheme as L.Control.Layers to store then retrieve a reference to the corresponding layer.

L.Control.Layers stores just the layer's leaflet_id as a property of the input element: input.layerId = L.stamp(obj.layer);

All layers known by the Layers Control (i.e. added at instantiation or later on by addBaseLayer or addOverlay) are referenced in this._layers object.

On input click event, the Layers Control loops through all inputs and retrieves the corresponding layers(s): obj = this._layers[input.layerId];

So in your case, if you use the same scheme, store reference to all layers of interest in this._layers, attach the id the input refers to with input.layerId = L.stamp(layer), then retrieve it when needed by reading that id and getting the corresponding reference:

_onInputClick: function (event){
    var id = event.currentTarget.layerId;
    var layer = this._layers[id];

This is an old thread... but I found a good answer to a similar problem here: https://stackoverflow.com/questions/33740996/how-to-get-the-baselayer-selected-in-leaflet

My situation dealt with base layers, but the hooks are there to chart events for adding an overlay (overlayadd) or removing an overlay (overlayremove). See here: https://leafletjs.com/reference-1.6.0.html#map-overlayadd

Here is my working code that returns the layer_id from layer control when a baselayer change event occurs. Using this layer_id I can determine which base layer is currently turned on:

map.on('baselayerchange', function (e) {
   currentLayerID = e.layer._leaflet_id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.