In order for something to happen when you click/hover/drag on the map or a feature, such as a point or polyline, you need to add map events.
The following code shows a popup containing the lat/long location of the mouse when the user clicks on the map:
function onMapClick(e) {
alert("You clicked the map at " + e.latlng);
}
map.on('click', onMapClick);
The leaflet quick start guide provides additional examples.
Here's another simple example from a recent app I created:
This code 'highlights' the selected layer (based on the layer ID) by setting the layer style
function highlightLayer(layerID) {
map._layers['name'+LayerID].setStyle(highlight);
}
This bit of code is simple a variable containing the style I want the feature to possess when it is highlighted.
var highlight = {
'color': '#333333',
'weight': 2,
'opacity': 1
};
You can fire this code using the events mentioned above (click, hover, etc.)
Best Answer
Here is the basic idea, set a style for the line, and set a style for the highlight, then in your forEachFeature function, set the layer on click to highlight. My example below resets the style in case, you clicked on one before.
Here is a working example for a polygon layer. http://www.gistechsolutions.com/leaflet/DEMO/basic/basic_Poly.html