Home >Web Front-end >JS Tutorial >How to use remove in js

How to use remove in js

anonymity
anonymityOriginal
2019-05-29 13:27:0218906browse

remove() method is used to remove an option from a drop-down list.

How to use remove in js

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(&#39;remove&#39;); 
for (var i = re.length-1;i >=0;i--) { 
re[i].remove(); 
console.log(i); 
}

Never delete like this

var re = document.getElementsByClassName(&#39;remove&#39;); 
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn