I want to add ID to each element of class .content
, and I want each ID to have integer increase by 1. Example:
Use this
in the each loop :
$(".content").each(function(index) {
this.id = 'content_' + index;
});
Otherwise you are selecting all the elements with class .content
JS only approach:
var content = document.querySelectorAll('.content');
[].forEach.call(content, function(item, index) {
item.id = "content_" + (index+1);
});
ES6/ES2015 syntax:
let content = document.querySelectorAll('.content');
[].forEach.call(content, (item, index) => item.id = `content_${(index+1)}`);