Problem using querySelector with the filter() function

I tried to create a function that will change the background-color of all class-list items whenever a span of the specified className is hovered over.

Problems are:

  1. It works only whenever a single <span> was hovered – while I need it to happen whenever any <span> is hovered.

  2. It does not work with the last group of spans for some reason.

I assume there are much better ways to have written the code – sorry, I am new to JavaScript.

Thank you very much!

The codes:

const array = [...document.getElementsByClassName('important')];
console.log(array);
console.log('---- ---- ---- ----');

// NEW ATTEMPT:

// NOTE: I copied that one from another code which had worked perfectly;
// I.E.: element.filter(item => (item !== e.target));

for (let i = 0; i < array.length; i++) {
    array[i].onmouseover = (e) => {
        console.log(`block ${i} was hovered over.`);

        // const arrayBlock = e.target;
        // arrayBlock.id = ``;

        let marking = array.filter(item => (item.querySelectorAll === `span.color-A${i}`));

        for (let all of marking) {
            all.style.backgroundColor = "red";
        };
    }
};

// NEW ATTEMPT ^^

// OLD ATTEMPT:

const markerContainer = document.getElementById('marker-container'); // Created because 'querySelector' couldn't be set upon const 'array';
console.log(markerContainer.children);
console.log('---- ---- ---- ----');

for (let i = 0; i < array.length; i++) {
    const arrayBlock = markerContainer.querySelector(`.block-A${i}`);
    
    console.log(arrayBlock);
    console.log('---- ---- ---- ----');

    const marking = arrayBlock.querySelectorAll(`span.color-A${i}`);

    console.log(marking);
    console.log('---- ---- ---- ----');

    marking[i].onmouseover = () => { // GETS AN ERROR: "TypeError: Cannot set properties of undefined (setting 'onmouseover')";
        for (let all of marking) {
            all.classList.remove(`A${i}-leave`);
            all.classList.add(`A${i}-enter`);
        };
    }

    marking[i].onmouseout = () => {
        for (let all of marking) {
            all.classList.remove(`A${i}-enter`);
            all.classList.add(`A${i}-leave`);
        };
    }
};

// OLD ATTEMPT ^^
.color-A0 {
    background-color: white;
    color: black;
}

.A0-enter {
    animation: A0-enter 0.4s ease 0s 1 normal forwards;
}

@keyframes A0-enter {
    0% {
        background-color: red;
        color: black;
    }

    100% {
        background-color: red;
        color: white;
    }
}

.A0-leave {
    animation: A0-leave 0.4s ease 0s 1 normal forwards;
}

@keyframes A0-leave {
    0% {
        color: black;
        background-color: red;
    }

    100% {
        background-color: white;
    }
}

.color-A1 {
    background-color: white;
    color: black;
}

.A1-enter {
    animation: A1-enter 0.4s ease 0s 1 normal forwards;
}

@keyframes A1-enter {
    0% {
        background-color: blue;
        color: black;
    }

    100% {
        background-color: blue;
        color: white;
    }
}

.A1-leave {
    animation: A1-leave 0.4s ease 0s 1 normal forwards;
}

@keyframes A1-leave {
    0% {
        color: black;
        background-color: blue;
    }

    100% {
        background-color: white;
    }
}

.color-A2 {
    background-color: white;
    color: black;
}

.A2-enter {
    animation: A2-enter 0.4s ease 0s 1 normal forwards;
}

@keyframes A2-enter {
    0% {
        background-color: green;
        color: black;
    }

    100% {
        background-color: green;
        color: white;
    }
}

.A2-leave {
    animation: A2-leave 0.4s ease 0s 1 normal forwards;
}

@keyframes A2-leave {
    0% {
        color: black;
        background-color: green;
    }

    100% {
        background-color: white;
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Document</title>

    <style>
        body {
            text-align: left;
            padding-top: 10%;
            padding-left: 10%;

            font-family: sans-serif;
            font-size: 40px;
        }
    </style>

    <link rel="stylesheet" href="a.css">
</head>

<body>
    <div id="marker-container">

        <span class="important block-A0">
            <p>

                <span class="color-A0">
                    Important Text Number A1 (Related to: Red)</span>
                -
                <span class="color-A0">
                    Important Text Number A2 (Related to: Red)</span>

                BLAH BLAH BLAH

                <span class="color-A0">
                    Important Text Number A3 (Related to: Red)</span>

                BLAH BLAH BLAH
                -
                <span class="color-A0">
                    Important Text Number A4 (Related to: Red)</span>

                Summary.

            </p>
        </span>

        <span class="important block-A1">
            <p>

                BLAH BLAH BLAH

                <span class="color-A1">
                    Important Text Number B1 (Related to: Red)</span>
                -
                BLAH BLAH BLAH

                <span class="color-A1">
                    Important Text Number B2 (Related to: Red)</span>

                Summary.

            </p>
        </span>

        <span class="important block-A2">
            <p>

                <span class="color-A2">
                    Important Text Number C1 (Related to: Red)</span>

                BLAH BLAH BLAH

                -
                BLAH BLAH BLAH

                <span class="color-A2">
                    Important Text Number C2 (Related to: Red)</span>

                Summary.

            </p>
        </span>

    </div>

    <script src="a.js"></script>
</body>

</html>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật