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

386
Views
Uncaught TypeError: Cannot read properties of undefined (reading 'target')

I have a made a function that replaces a base64 string with the matching email address when clicking on some text.

This works, but for some reason I get this error: Uncaught TypeError: Cannot read properties of undefined (reading 'target').

JS

initEmails() {
    const emailToggles = document.querySelectorAll('.email');
    if (!emailToggles) {
        return;
    }

    emailToggles.forEach(toggle => {
        toggle.addEventListener('click', this.replaceEmail);
    });
}

replaceEmail(ev){
    const target = ev.target.closest('.email');

    if (!target.classList.contains('active')) {
        ev.preventDefault();
    }

    const email = atob(target.dataset.value);

    target.setAttribute('href', 'mailto:' + email);
    target.querySelector('.replace').innerText = email;

    target.classList.add('active');
}

HTML:

<span>
    <h5>@lang('E-mail')</h5>
    <a href="javascript:void(0);" data-value="{{ base64_encode($employee->email) }}" class="email">
        <span class="replace">Toon e-mailadres</span>
    </a>
</span>

I tried checking if .email exists before initialising the functions like this:

if (document.querySelector('.email') !== null) {
    this.initEmails();
    this.replaceEmail();
}

But the error keeps popping up in my console.

What can I do to fix it?

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!