remove() method is used to remove an option from a drop-down list.
Syntax
selectObject.remove(index)
index -- Required: Specifies the options to be deleted index number.
Description
This method removes the
The following example can delete the selected option from the list:
<html> <head> <script type="text/javascript"> function removeOption() { var x=document.getElementById("mySelect") x.remove(x.selectedIndex) } </script> </head> <body> <form> <select id="mySelect"> <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option> </select> <input type="button" onclick="removeOption()" value="Remove option"> </form> </body> </html>
Note: When deleting a large number of nodes, be careful when deleting in a loop. Delete carefully, do not delete from small to large, otherwise the deletion will be incomplete.
var re = document.getElementsByClassName('remove'); for (var i = re.length-1;i >=0;i--) { re[i].remove(); console.log(i); }
Never delete like this
var re = document.getElementsByClassName('remove'); for (var i = 0;i <re.length;i++) { re[i].remove(); console.log(i); }
The problem of incomplete deletion will occur
The above is the detailed content of How to use remove in js. For more information, please follow other related articles on the PHP Chinese website!