หากต้องการลบตัวเลือก (options) จาก Dropdown List ให้ใช้เมธอด remove() ใน JavaScript ลองรันโค้ดด้านล่างเพื่อเรียนรู้วิธีลบตัวเลือกจาก Drop-down:
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<form id="myForm">
<select id="selectNow">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
<input type="button" onclick="remove()" value="Click to Remove">
</form>
<p>Select and click the button to remove the selected option.</p>
<script>
function remove() {
var x = document.getElementById("selectNow");
x.remove(x.selectedIndex);
}
</script>
</body>
</html>
select.remove(index) ลบตัวเลือกที่ดัชนี index (0-based) โค้ดใช้ x.selectedIndex เพื่อดึงดัชนีของตัวเลือกที่ถูกเลือกอยู่ หากไม่มีตัวเลือกใดถูกเลือก selectedIndex จะเป็น -1 การเรียก remove(-1) จะไม่เกิดอะไรขึ้น (ปลอดภัย)