[GIS] How to enable a Leaflet Draw tool programatically

leafletleaflet-draw

I'm using the Leaflet Draw control, and would like to enable/activate a particular tool (Draw Polygon) using JavaScript, so that this tool is active when the page loads. Is this possible?

enter image description here

https://stackoverflow.com/a/17144318/906814 shows how to bypass the control and start drawing directly, but I'd like instead to programatically enable the existing tool since it gives easy access to additional functionality.

Best Answer

Simply trigger a "click" event on the "Draw a polygon" toolbar button (it has a specific class "leaflet-draw-draw-polygon"):

document.querySelector(".leaflet-draw-draw-polygon").click();

Demo: http://playground-leaflet.rhcloud.com/zaze/1/edit?html,output