Skip to content
Advertisement

How to add a button to run code inside LeafletJS marker?

I am trying to add a button inside a pointer which print a log to the console. This is just meant to be a test, so I can actually make the marker run a method, but I can’t even get it to print text.

JavaScript

When I load the page, I immediately get this error:

Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘addEventListener’)

The console says this error is caused by

JavaScript

but I’m not sure why it’s null. Can anyone help me out?

Advertisement

Answer

You are creating the button incorrectly.

It will be right this way:

JavaScript

In order to add a button to the page, you need to find the desired parent element and add the button as a child element:

JavaScript

Next, you can work with the button as you need:

JavaScript

Result code example:

JavaScript

More about createElement

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement