본문 바로가기

드롭다운 목록 활용

드롭다운은 기본적으로 select 태그로 구현 

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
document.addEventListener('DOMContentLoaded', () => {
const select = document.querySelector('select')
const p = document.querySelector('p')
p.style.font = '15px arial'
select.addEventListener('change', (event) => {
const options = event.currentTarget.options
const index = event.currentTarget.options.selectedIndex
p.textContent = `선택: ${options[index].textContent}`
})
})
</script>
</head>
<body>
<select>
<option>떡볶이</option>
<option>순대</option>
<option>오뎅</option>
<option>튀김</option>
<option>김밥</option>
</select>
<p>선택: 떡볶이</p>
</body>
</html>
view raw select hosted with ❤ by GitHub

'' 카테고리의 다른 글