rob.m rob.m - 1 year ago 148
Javascript Question

Add class on click to a path with leaflet js

I am trying to add a class to the clicked polygon using:

function addClass() {
function style(feature) {
return {
className: "active"

function onEachFeature(feature, layer) {
click: addClass


Docs here

Answer Source

The function app in your code only creates a function (and nothing more). This function is only created, but never fired. Another problem is that this function does nothing that relevant to the element that was clicked.

Here is the change you are looking for:

function app(e) {

Here is a working codepen (based on your code):

I added the active class to the CSS so you can actually see the change on the screen

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download