๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ดํŽ™ํŠธ

2. ์„œ์น˜ ์ดํŽ™ํŠธ -1

by Youcodein 2022. 8. 17.
728x90
๋ฐ˜์‘ํ˜•

์„œ์น˜ ์ดํŽ™ํŠธ ๋งŒ๋“ค๊ธฐ1

indexOf()๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ์น˜ ์ดํŽ™ํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜์„ธ์š”!


์„œ์น˜ ์ดํŽ™ํŠธ1

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

//์„ ํƒ์ž
const searchBox = document.querySelector(".search__box input");            //๊ฒ€์ƒ‰ ์˜์—ญ
const searchList = document.querySelectorAll(".search__list ul li"); //๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ

const searchInfo = document.querySelector(".search__info .num")


//์ „์ฒด ๊ฐฏ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length;

//๊ฒ€์ƒ‰ ์˜์—ญ
searchBox.addEventListener("keyup", () => {
    const searchWord = searchBox.value;     //์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ‚ค์›Œ๋“œ
    searchList.forEach(el => {
        const cssName = el.dataset.name;
        if (cssName.indexOf(searchWord)) {
            el.classList.add("hide");
        } else {
            el.classList.remove("hide");    //indexOf๊ฐ’์ด 0์ด ๋˜๋Š” ๊ฐ’๋งŒ hide๋ฅผ ์ง€์›Œ์ค๋‹ˆ๋‹ค.
        }
    });
});

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€