Lấy các mục của danh sách dropdown bằng JavaScript
Giả sử chúng ta có một danh sách dropdown:
<select id="select">
<option value="one">một</option>
<option value="two">hai</option>
<option value="three">ba</option>
</select>
Câu hỏi: làm thế nào để chúng ta lấy tất cả các mục của danh sách này? Cách đơn giản nhất là sử dụng bộ chọn tương ứng:
let options = document.querySelectorAll('#select option');
console.log(options);
Có thể không tìm kiếm trong toàn bộ tài liệu, mà cụ thể trong select của chúng ta:
let select = document.querySelector('#select');
let options = select.querySelectorAll('option');
console.log(options);