Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

376
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda