Home >Web Front-end >JS Tutorial >How to use remove in js
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 5a07473c87748fb1bf73f23d45547ab8 element from the specified position in the options array. If the specified index is less than 0, or greater than or equal to the number of options, the remove() method ignores it and does nothing.
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!