I am trying to learn stuff I was used to doing in jQuery in plain JavaScript.
I have an example I found on the internet to solve, which gave me a hard time.
I am trying to remove the parent div. single on click on the button. remove.
Here is the code;
<div class="row" id="showAward">
</div>
<div class="w-100 text-center">
<a id="add" class="bg-primary text-white px-3 py-2 rounded-circle cursor-pointer">
<i class="fa-solid fa-plus"></i>
</a>
</div>
<script>
let count = 0;
document.addEventListener('click', function(event) {
if (event.target.id === 'add') {
count++;
addAward(count);
}
if (event.target.classList.contains('delete')) {
event.currentTarget.parentNode.remove();
}
});
function addAward(number) {
let html = `<div class="col-lg-4 mb-3 position-relative">
<label for="image${number}" class="form-label">image ${number}</label>
<input type="file" class="form-control" id="image${number}" name="image${number}">
<a class="delete text-danger cursor-pointer position-absolute end-0 top-0"><i class="fa-solid fa-times"></i></a>
</div>`;
document.getElementById('showAward').insertAdjacentHTML('beforeend', html);
}
</script>
2
Answers
It looks like, when the event listener is on
document
the property curretnTarget isundefined
. Useevent.target
instead. And then instead of usingparentNode
use the method closest(). You can give a CSS selector as a parameter to the method and get a parent element.Event
‘scurrentTarget
getter will return theEventTarget
in the bubbling chain where the event is being dispatched at the current phase.It will change dynamically and when gotten synchronously from a callback to
EventTarget.addEventListener()
, it will be theEventTarget
on which you calledaddEventListener()
(which makes it similar tothis
in this regard).So in your case
currentTarget
in that context would the theDocument
node, because you did calldocument.addEventListener()
.Now,
.target
would represent theEventTarget
on which the event has been dispatched, i.e. in case of a click, the element that did receive the click.However, it may not be the one you were expecting. For instance in you case, clicking the
<i>
element that is inside the.delete
one, would set the.target
to that<i>
element, and not to the.delete
one and thus you’d miss the click.Instead when delegating use
.closest()
to test if the element you’re actually interested in is in the event chain. This method also returns the element in which it’s been called in case it matches.