Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Javascript undefined element on adding EventListener

Have a class:

'use strict'

class modal{
    modal_wnd;
    close;
    constructor(){
        this._init();
    }

    _init(){
        this.modal_wnd = document.getElementById('modal'); //div
        this.close = document.getElementById('mdl_close'); // span 'X'
        this.close.addEventListener('click', this.close_1);
    }

    show(){
        this.modal_wnd.style.visibility = 'visible';
    }

    close_1(){
        alert(this.close);
        this.modal_wnd.style.visibility = 'hidden';
    }
}

Have div-element 'modal' and span 'X' in it. On a page have a button to show div:

bt_modal.addEventListener("click", function(){
                    let mdl = new modal();
                    mdl.show();
                });

that works fine, but when I press 'X' - get such an error: Uncaught TypeError: Cannot read properties of undefined (reading 'style') at HTMLSpanElement.close_1. Can anyone explain why does it happen?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!