Home > Web Front-end > JS Tutorial > body text

How to use remove in js

anonymity
Release: 2019-05-29 13:27:02
Original
18836 people have browsed it

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

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>
Copy after login

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); 
}
Copy after login

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); 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!