<div class="leaflet-marker-icon"></div>
<div class="leaflet-marker-icon"></div>
<div class="leaflet-marker-icon"></div>
<div class="leaflet-marker-icon"></div>
The map generates these divs, all with the same class leaflet-marker-icon
. I need to assign a unique id
attribute to each div (e.g. id=0
, id=1
, id=2
, id=3
).
This is my code:
var length = $('.leaflet-marker-icon').length;
for (var x = 0; x < length; x++) {
$('.leaflet-marker-icon').eq(x).attr('id', x);
}
Within the loop, using
$('.leaflet-marker-icon').attr('id', x)
assigns the same id to all elements due to it being a class selector. Can someone suggest how to assign unique ids to each element?