I want to delete some elements of an array by using data-object-id attribute.
But when I use it, the loop does not run as expected. It seems like it jumps over an element every time.
var banIds = ["111", "222", "333"];
function toggle_visibility(className) {
var elements = document.getElementsByClassName(className),
n = elements.length;
for (var i = 0; i < n; i++) {
var e = elements[i];
var statusID = e.getAttribute('data-object-id');
if ($.inArray(statusID, banIds) !== -1) {
e.remove();
}
}
}
What's the problem? And how do I solve it ?
document.getElementsByClassName(...)returns HTMLCollection - array like object, but not an array ... So it is updated dynamically while you are deleting your elements.