1

The code below sets the background of an element when it gets focus:

document.addEventListener(
        'focusin',
        (event) => {
          if (event.target !== null && event.target.parentElement.classList.contains('note')) {
            event.target.parentElement.style.backgroundColor = "#E6FBFF"
          }
        })

It works on my browser (Chrome).

The IDE and build process raise an error:

ERROR in src/components/Note.vue:146:98
TS2339: Property 'parentElement' does not exist on type 'EventTarget'.
    144 |           }
    145 |         })
  > 146 |       document.addEventListener('focusout', (event) => {if (event.target !== null) {event.target.parentElement.style.backgroundColor = "white"}})
        |                                                                                                  ^^^^^^^^^^^^^
    147 |     })
    148 |
    149 |     const setFocus = (event) => {

I d not understand the error (besides of course its nature), I was under the impression that event.target would give me a handler to the element the event (of type EventTarget) happened on.

How should I address this error?

WoJ
  • 27,165
  • 48
  • 180
  • 345

0 Answers0