Search code examples
javascriptleafletleaflet.drawleaflet-geoman

Accessing features from checkboxes that are inside Leaflet popup


I'm working in leaflet, and I'm drawing a polygon with the Leaflet PM plugin

On I finish the drawing I run the following event

mymap.on('pm:create', function(e) {
    var strPopup = '<input type="checkbox" id="cbx1" class="cbx" name="objects" value="eraser">';
    strPopup += '<input type="checkbox" id="cbx2" class="cbx" name="objects" value="pencil">';
    strPopup += '<button id="btnSave" class="btn" onclick="foundObjects()" >Save</button>';
    e.layer.bindPopup(strPopup);
    e.layer.openPopup();
}

So, when the user clicks the 'Save' button, I'm trying to read the values of the checkboxes with jQuery:

function foundObjects(){
    console.log( $('#cbx1').attr('checked'))
    console.log( $('#cbx2').attr('value'))
}

However both results appears in Console as 'undefined'. Someone has a hint, why is it happening?


Solution

  • I have this same issue and I was able to solve it using pure javascript

    function check() {
        document.getElementById("myCheck").checked = true;
    }
    
    function uncheck() {
        document.getElementById("myCheck").checked = false;
    }