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

firecode85

I am trying to add a button inside a Leaflet marker 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.

const marker = L.marker([latitude, longitude]).addTo(map);

const button = '<br/><button type="button" id="delete">Delete marker</button>'
const deletebutton = document.querySelector("#delete");
button1.addEventListener("click", function(event) {
    console.log('This button works!');
});
marker.bindPopup(button);

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

button1.addEventListener("click", function(event) {

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

wowandy

You are creating the button incorrectly.

It will be right this way:

const button = document.createElement('button');

button.id = 'delete';
button.textContent = 'Delete marker';

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:

// replace this id with what you need
const buttonParrentId = 'button-parrent-id';

const buttonParrent = document.getElementById(buttonParrentId);
buttonParrent.appendChild(button);

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

const marker = L.marker([latitude, longitude]).addTo(map);

button.addEventListener("click", function(event) {
    console.log('This button works!');
});

marker.bindPopup(button);

Result code example:

const button = document.createElement('button');

button.id = 'delete';
button.textContent = 'Delete marker';

// replace this id with what you need
const buttonParrentId = 'button-parrent-id';

const buttonParrent = document.getElementById(buttonParrentId);
buttonParrent.appendChild(button);

const marker = L.marker([latitude, longitude]).addTo(map);

button.addEventListener("click", function(event) {
    console.log('This button works!');
});

marker.bindPopup(button);

More about createElement

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Leafletjs: how to access and update a marker inside a layer

How to add a ProgressBar and a button underneath a marker

How to add class to button of selected marker

How to add delete button to a marker on a map

How to add Vue router-link inside of Leaflet marker popup

In Flutter, how to add a marker on openstreetmap by tapping a screen or clicking a button?

How to add a button on React Leaflet map to create a marker?

How to call Javascript code from ScalaJS leafletJS

How can I add a button inside var content page code-behind using Xamarin.Forms?

How to add collapsible form to edit-button inside PHP code? I tried something, but didn't work

How to add a clickable button inside an angular textarea

How to add play button inside responsive slide

How to add a button inside a react bootstrap table?

How to add the original close button inside iFrame?

How to add button inside textarea using Javascript?

how to add progress dialog inside button

How to add a button inside a input field

How to add button inside input field

React Native how to add button inside Flatlist?

How to Add Marker in osmdroid?

How to run executable with Python code inside

How to run php code inside cordova?

How to run code on server on the click of button?

how to run JS code when button is clicked

How to run code on a button click in android fragment

Add php code inside xml that make xml run in one domain

How to add a icon inside a button that shape oval button

Add button inside textbox

How to add a second button code to MainActivity