Variable data attribute not working

225 views Asked by At

I have this code:

for(var i = 0; i < subjects.length; i++){
$('#container').append('<div class="content"><span class="context-text" data-item="' + i '">' + subject[i].title + '</span></div>');
}

It is not working. This does work:

for(var i = 0; i < subjects.length; i++){
$('#container').append('<div class="content"><span class="context-text" data-item="1">' + subject[i].title + '</span></div>');
}

Can anybody help? I am missing something and I don't see what it is.

1

There are 1 answers

1
Kiran Shinde On BEST ANSWER

Missed + after i

Change this line

$('#container').append('<div class="content"><span class="context-text" data-item="' + i '">' + subject[i].title + '</span></div>');

to

$('#container').append('<div class="content"><span class="context-text" data-item="' + i + '">' + subject[i].title + '</span></div>');