I have an array: 'imageIds':
imageIds = ["778", "779", "780", "781", "782"];
I want to find all the elements of the .preview-image class on the page, from which I know that the number will correspond to the length of the array.
Then I want to assign the data 'data-img-id' attribute with the value imageIds [0] to the first matching element, the second matching element with imageIds [1], etc.
So, the end result will convert this:
<div class="preview-image">...</div>
<div class="preview-image">...</div>
<div class="preview-image">...</div> etc
in that:
<div class="preview-image" data-img-id="778">...</div>
<div class="preview-image" data-img-id="779">...</div>
<div class="preview-image" data-img-id="780">...</div> etc
Not quite sure how to form a cycle that will achieve this.
source
share