Passing In event.target as an Input of Node.remove Function

I have a bunch of DIVs which do not have IDs or Classes associated with them on my page. I would like to remove any DIV from the DOM simply by I clicking on it so I tried using Node.remove where Node is the clicked DIV. I tried using

document.querySelector(event.target).remove();

but it didn’t work. If I give an ID to each of the DIVs it does work but I would like to know I how to make it work without adding IDs or Classes to the DIVs. Please see my code here .

querySelector expects a query string, that can either be the name of an element, a CSS selector, or an ID reference. The querySelector then gives you a reference to the first element that matches the selector you gave.

You don’t need to do any of that, because you already have a reference to the element, in the event.target property.

Thanks Paul, That worked. I simply used event.target and the remove function works.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.