Click Events with Leaflet and geoJSON
You were on the right way with onEachFeature
.
It's just you have to bind event click on each element.
See below (tested)
function whenClicked(e) {
// e = event
console.log(e);
// You can make your ajax call declaration here
//$.ajax(...
}
function onEachFeature(feature, layer) {
//bind click
layer.on({
click: whenClicked
});
}
geojson = L.geoJson(your_data, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
You can do it with slightly less code than ThomasG77's version:
function onEachFeature(feature, layer) {
//bind click
layer.on('click', function (e) {
// e = event
console.log(e);
// You can make your ajax call declaration here
//$.ajax(...
});
}
geojson = L.geoJson(your_data, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
just another way as inline function
geojson = L.geoJson(your_data, {
style: style,
onEachFeature: function onEachFeature(feature, layer) {
layer.on('mouseover', function (e) {
// e = event
console.log(e);
// You can make your ajax call declaration here
//$.ajax(...
});}).addTo(map);