I have attached an event listener on 'contextmenu'
event. In the callback function I use:
e.currentTarget.getElementsByTagName('button')[0]
but I encounter the TypeError message: getElementsByTagName is not a function.
I checked if e.currentTarget
is an instance of Element
, It was. I checked if its nodeType
is 1
, It was. I logged it, it was the element I was expecting to be.
I checked if e.currentTarget.querySelector
works, it did. I checked querySelectorAll
, it worked as well. I checked the children
property, it worked as expected.
I checked getElementById
, and I got the same error.
When I access the element in the console directly (using document.querySelector('.object').getElementsByTagName('button')[0]
as a command) nothing bad happens, but when I try to getElementsByTagName
in the callback function for e.currentTarget
(which is the same as document.querySelector('.object')
in the previous line of code), I get this error. It’s like when I access the element using document and other methods, it has the so far mentioned method but when I access it using currentTarget, it doesn’t have the method.
As it seems, I’m facing a DOM element which doesn’t do good with getElementsByTagName
and getElementById
but is okay with querySelector
and querySelectorAll
.