[Solved] Short hand delete many getElementById in javascript


.remove() on a DOM element is a very new and experimental method (not supported in any version of IE, for example). You would probably be better off using .removeChild().

If you wanted to remove that many elements, I’d suggest you use a table and iterate through the table or if they are truly sequential ID values, you can manufacture the ids in a loop.

var item;
for (var i = 7; i <= 19; i++) {
    item = document.getElementById("bl" + i);
    item.parentNode.removeChild(item);
}

If you needed to do this frequently, then it may be easier to put a class name on all the objects that you want to operate on at once and get all of them in a query based on class name with something like document.querySelectorAll().

Or, you could create any number of short functions for operating on a set of ids:

function removeElements(idArray) {
    var item;
    for (var i = 0; i < idArray.length; i++) {
        item = document.getElementById(idArray[i]);
        if (item) {
            item.parentNode.removeChild(item);
        }
    }
}

3

solved Short hand delete many getElementById in javascript