How do I remove items?

  • 0
    I know there is an option:

    var deleteElement = document.getElementById("idDelete");
    deleteElement.remove();

    But it only removes by id. I need to remove all elements with class "downAB".
    How to do it?
    JavaScript Adalaide Hurley, Oct 6, 2020

  • 3 Answers
  • 0
    document.querySelectorAll('.downAB').forEach( item => item.remove())
    Anonymous

  • 0
    With the help of querySelector you can substitute any CSS selectors.

    querySelector - takes the first element

    querySelectorAll - all



    var deleteElement = document.querySelector("#idDelete");
    deleteElement.remove();




    Example:

    var deleteElements = document.querySelectorAll(".example");
    for (var i = 0; i < deleteElements.length; i++) {
    deleteElements[i].remove();
    }
    Greyson Campbell

  • 0
    const elems = document.querySelectorAll('.class');
    elems.forEach(i => {i.remove()})
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!