How js adds id to multiple div

<div class="wrap">
    <div class="test"></div>
    <div class="test"></div>
    <div class="test"></div>
</div>

I want to add id to each .test

becomes

<div class="test" id="test-1"></div>
<div class="test" id="test-2"></div>
<div class="test" id="test-3"></div>

how can this be achieved?

you can use js jquery

Mar.07,2021

$(".wrap .test").each(function(i) {
    $(this).attr('id', 'test-' + (i + 1))
})

change it directly by JS.

var eles = document.querySelectorAll('.wrap .test');

eles.forEach(function(v, i) {
    v.setAttribute('id', 'test-' + (i + 1))
})

var test=$(".test")
for(var i=0;i<test.length;iPP){
    test[i].id='test-' + (i + 1)
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5ba4a-44302.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5ba4a-44302.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?