Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove parent element on click with plain javascript

Tags:

javascript

I am trying to learn stuff I was used to do in jQuery do in plain JavaScript.

I have example I found on the internet to solve and it really gave me hard time.

I am trying to remove parent div.single on click on button.remove.

Here is the code;

 var btn = document.getElementsByClassName('remove')
    
 for (var i = 0; i < btn.length; i++) {
    btn[i].addEventListener('click',function (e) {      
      e.parentNode.parentNode.removeChild(e.parentNode);
    } , false);
 }
 <div class="single">
    <img src="http://via.placeholder.com/150x150">
    <button type="button" class="remove">X1</button>
  </div>
  <div class="single">
    <img src="http://via.placeholder.com/150x150">
    <button type="button" class="remove">X2</button>
  </div>
  <div class="single">
    <img src="http://via.placeholder.com/150x150">
    <button type="button" class="remove">X3</button>
  </div>
  <div class="single">
    <img src="http://via.placeholder.com/150x150">
    <button type="button" class="remove">X4</button>
  </div>

I am getting error e.parentNode is undefined.

Here is the jQuery code which does the same I am after.

$(document).ready(function() {  
  $(document).on('click', '.remove', function () {    
    $(this).parent('.single').remove()
  })
})

Thanks for any answers.

like image 726
iamwtk Avatar asked Oct 10 '17 11:10

iamwtk


People also ask

How to remove the parent element without removing its children in JavaScript?

To remove the parent element without removing its children, call the replaceWith () method on the parent element passing it the child nodes as a parameter, e.g. parent.replaceWith (...parent.childNodes). The replaceWith method replaces the element with a set of Node objects.

How to remove an element from the Dom with JavaScript?

As you might know, the DOM does not supports removing an element directly. When removing an element with JavaScript, you must go to its parent first instead.

How to remove the parent node of a node in Node JS?

function delete_row (e) { e.parentNode.parentNode.parentNode.removeChild (e.parentNode.parentNode); } You can now use node.remove () to remove the whole element so in your case you'd do

How to select an element by class name in JavaScript?

P. S. Beside these new methods, don’t forget about the querySelector () as well. That’s another cool JavaScript method which is very useful to select an element by class name for example. All these combined, provide a very good support for daily debugging and personal projects without using any framework.


Video Answer


2 Answers

To remove parent node as short way:

<div class="single">
   <img src="http://via.placeholder.com/150x150">
   <button type="button" onclick="return this.parentNode.remove();" class="remove">X</button>
</div>
like image 187
Tarek Kalaji Avatar answered Sep 18 '22 17:09

Tarek Kalaji


You need to get the element reference from the event object(currentTarget/target)

Note: All the modern browser's have support for Node.remove()

var btn = document.getElementsByClassName('remove')

for (var i = 0; i < btn.length; i++) {
  btn[i].addEventListener('click', function(e) {
    e.currentTarget.parentNode.remove();
    //this.closest('.single').remove() // in modern browsers in complex dom structure
    //this.parentNode.remove(); //this refers to the current target element 
    //e.target.parentNode.parentNode.removeChild(e.target.parentNode);
  }, false);
}
<div class="single">
  <img src="http://via.placeholder.com/150x150">
  <button type="button" class="remove">X1</button>
</div>
<div class="single">
  <img src="http://via.placeholder.com/150x150">
  <button type="button" class="remove">X2</button>
</div>
<div class="single">
  <img src="http://via.placeholder.com/150x150">
  <button type="button" class="remove">X3</button>
</div>
<div class="single">
  <img src="http://via.placeholder.com/150x150">
  <button type="button" class="remove">X4</button>
</div>
like image 20
Arun P Johny Avatar answered Sep 18 '22 17:09

Arun P Johny