I simply want to do this with my
var tag = evt.target.tagName.toLowerCase();
Event.target is of type
EventTarget, it does not inherit from
Element. So I have to cast it like this:
var tag = (<Element>evt.target).tagName.toLowerCase();
This is probably due to some browsers not following standards, right? What is the correct browser-agnostic implementation in TypeScript?
P.S. I am using jQuery to capture the
It doesn’t inherit from
Element because not all event targets are elements.
Element, document, and window are the most common event targets, but other objects can be event targets too, for example XMLHttpRequest, AudioNode, AudioContext, and others.
KeyboardEvent you’re trying to use can occur on a DOM element or on the window object (and theoretically on other things), so right there it wouldn’t make sense for
evt.target to be defined as an
If it is an event on a DOM element, then I would say that you can safely assume
evt.target. is an
Element. I don’t think this is an matter of cross-browser behavior. Merely that
EventTarget is a more abstract interface than
Further reading: https://github.com/Microsoft/TypeScript/issues/29540
Answered By – JLRishe