Search code examples
javascriptleafletleaflet.draw

How to trigger end of editing with button?


I am currently using the code below in a custom button to trigger editing mode in Leaflet.

I would like to now use more custom buttons to save cancel and clear all.

My problem is that neither of these have classes in the leaflet control panel to select, so I cannot use the method below.

What can I do?

document.querySelector(".leaflet-draw-edit-remove").click();

Solution

  • Try this!

    var simulateClick = function (elem) {
        // Create our event (with options)
        var evt = new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window
        });
        // If cancelled, don't dispatch our event
        var canceled = !elem.dispatchEvent(evt);
    };
    var someLink = document.querySelector('a');
    simulateClick(someLink);
    

    If it works where the explanation https://gomakethings.com/how-to-simulate-a-click-event-with-javascript/