How to remove encircle element using javascript without target id or class????
Like that
<div>
Choice 1 <span>close X</span>
</div>
I hope that when someone click the "close X" word, it will delete Choice 1 and close X
I try to
<div>
Choice 1 <span onclick="remove(this)">close X</span>
</div>
<script>
function remove(val) {
//val.remove(); Just remove the "close X" word
//val.previousElementSibling.remove(); //Not work
//$(this).prev().remove(); using Jquery //Still Not work
}
</script>
Anyidea How to do it?? Thank you very much
2
Answers
Try
event.target.parentElement.remove()
Basically, use the
addEventListener()
method on the "Close X" element to capture clicks then iterate over the child nodes of the parent<div>
element then remove any text nodes found.You can actually shorten it a bit by using some arrow functions.