paradite paradite - 4 months ago 12
Javascript Question

br element with no parent node triggers paste event

I am having trouble getting the parent node of a

br
element which triggered a
ClipboardEvent
.

What I am trying to achieve is to recursively match its parent to a certain class, but the
br
element's
parentNode
property is
null
:

br element

Here is the DOM tree:

br element in DOM tree

The path property of the
ClipboardEvent
seems to capture the hierarchy but it is not standard:
Event path

Update:

I figured out why this is happening.

The
br
element was replaced by some new element upon pasting by the first event handler, and in my 2nd event handler(this one that I am having issue with),
br
is no longer in DOM tree.

Answer

Try e.target.parentElement

e.target will get the target of the event, in this case the <br>

Demo: https://codecanister.com/Project/d547eed9/6/result/