lel lel - 3 years ago 176
Javascript Question

Creating unique ids and assigned them in html for use Javascript

i am currently trying to create unique ids in html.
However, i am not sure if this below codes is creating unqiue ids and being assigned to the element itself in the html or just creating unqiue ids only.
Please help me out. If this is not assigned them in html, how am i supposed to do them?

var abcElements = document.querySelectorAll('.like');

for (var i = 0; i < abcElements.length; i++){

abcElements[i].id = 'like_' + i;
alert(abcElements[i].id);
}


Updates:
I have include all the related codes in

<td><a onclick="return confirm(this)"><input class="switch" data-toggle="toggle" data-on="like" data-off="unlike" data-onstyle="danger" data-width="90" data-height="30" type="checkbox"></a></td>

<td style="display:none;"><div class="like">${like}</div></td>


Javascript

var abcElements = document.querySelectorAll('.switch');
var status = document.querySelectorAll('.like');
alert(status.length);
for (var i = 0; i < abcElements.length; i++)
{

abcElements[i].id = 'switch_' + i;
alert(abcElements[i].id);

for (var j = 0; j < status.length; j++)
{
status[j].id = 'like_' + j;

var statusText = $('#'+status[j].id+'').html();
alert(statusText);
if(statusText == 'true' )
{
$('#'+abcElements[i].id+'').bootstrapToggle('on');
}


}

}

lel lel
Answer Source

I realized i am assigning the ids for individual divs. I just need to move one of the loops outside that's all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download