mymaps = {} mymarkers = {} $(document).ready(function () { $('.OSMap').each(function (i, el) { let lon = $(el).attr('lon') let lat = $(el).attr('lat') let sectionId = el.id.split("-")[1] renderMap = function (i,lat, lon) { mymaps[sectionId] = L.map('mapid-'+sectionId).setView([lat, lon], 15); L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', { maxZoom: 19, attribution: '© OpenStreetMap, Tiles courtesy of Humanitarian OpenStreetMap Team' }).addTo(mymaps[sectionId] ); mymarkers[sectionId] = L.marker([lat, lon]).addTo(mymaps[sectionId] ); } renderMap(i, lat, lon) }) })