Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript remove elements by class name

Tags:

javascript

dom

How we can remove the all div by same name of class.

 <input class="form-control" type="text" name="option[]"><input class="form-control" type="text" name="option[]"><input class="form-control" type="text" name="option[]">
like image 504
Bilal Siddiq Avatar asked Dec 03 '22 21:12

Bilal Siddiq


2 Answers

Try with querySelectorAll() And NodeList#forEach use to iterate the element

document.querySelectorAll('.classname').forEach(function(a){
a.remove()
})

Example snippet

document.querySelectorAll('.form-control').forEach(function(a) {
  a.remove()
})
<input class="form-control" type="text" name="option[]">
<input class="form-control" type="text" name="option[]">
<input class="form" type="text" name="option[]" value="not same class">
<input class="form-control" type="text" name="option[]">
like image 105
prasanth Avatar answered Dec 06 '22 12:12

prasanth


My approach might be this

var elements = document.querySelectorAll('.test');  
for (var element of elements) {
   element.remove();
   // or 
   // element.parentNode.removeChild(element);
}

Hope it helps.

like image 32
Fered Avatar answered Dec 06 '22 10:12

Fered