charAt 을 이용한 실습
'javascript'.charAt(0) // j
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="/src/styles.css" />
</head>
<body>
<label>지역명을 입력해주세요. <input type="text" id="search" /></label>
<div id="list">
<button data-name="속초">속초</button>
<button data-name="부산">부산</button>
<button data-name="포항">포항</button>
<button data-name="서울">서울</button>
<button data-name="제주">제주</button>
</div>
<script src="src/index.js"></script>
</body>
</html>
js에서 element.classList 를 이용하는것도 point!!
const search = document.querySelector("#search");
const list = document.querySelectorAll("#list button");
search.addEventListener("keyup", () => {
const searchWord = search.value;
list.forEach((element) => {
if (!searchWord || searchWord === "") {
element.classList.remove("hide");
// console.log(element);
return;
}
const dataName = element.dataset.name;
//검색어가 일치할때
if (searchWord.charAt(0) === dataName.charAt(0)) {
// console.log(searchWord.charAt(0))
// console.log(dataName.charAt(0));
element.classList.remove("hide");
} else {
// 검색어가 일치하지 않았을때
element.classList.add("hide");
}
});
});