I want to set the style of the geojson function by setting it className. This works great if placed directly on such a function:
L.geoJson(geojson, {
onEachFeature: function (feature, layer) {
layer.setStyle({className: 'grid-cell'});
}
}).addTo(map);
with the style defined in the .css file
path.grid-cell{
stroke-opacity: 1;
stroke: #444;
fill: #aaa;
}
However, it does not work if it is added to the function event callback:
L.geoJson(geojson, {
onEachFeature: function (feature, layer) {
layer.on('click', function(e){
this.setStyle({className: 'grid-cell'});
this.bringToFront();
});
}
}).addTo(map);
Surprisingly, it setStyle({<style_options>});works anyway for any other L.path except className, for example. color, fillOpacity, weightEtc.
eg.
L.geoJson(geojson, {
onEachFeature: function (feature, layer) {
layer.setStyle({color: '#faa', fillOpacity: 0.4, weight: 1});
layer.setStyle({className: 'grid-cell'});
layer.on('click', function(e){
layer.setStyle({color: '#afa', fillOpacity: 0.4, weight: 2});
this.setStyle({className: 'grid-cell'});
this.bringToFront();
});
}
}).addTo(map);
Any idea what's here? Shown here is a plunker illustrating the problem.
source
share