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

159
Visualizações
Avoid that Vue autotriggers onclick method

I have been working around a span that is generated by Vue´s (version 2.2) method, and my problem is that, when the span is created, the method inside onclick is automatically launched. So, I'm looking for a way to avoid this behaviour and make this span only clickable by the user.

fnLikeIcon(colName, rowData) {
                let currentStatus = '';
                if (rowData.isLike) {
                    currentStatus = `<span class="mdi mdi-ok" onclick=${this.changeFavouriteProperty(colName, rowData)}"></span>`;
                } else {
                    currentStatus = `<span class="mdi mdi-outline" onclick=${this.changeLikeProperty(colName, rowData)}"></span>`;
                }
                return currentStatus;
            }

This above methods, calls this other one.

changeLikeProperty(colName, rowData) {
                if (rowData.isLike) {
                    this.unmarkAsLike(rowData);
                } else {
                    this.markAsLike(rowData);
                }
            }

I tried with .prevent() after onclick, but really I'm not sure why this behaviour happens. Thanks in advance!!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's because you actually run the function while generating the string for your span. And you bind to onclick the result of that function, not the function itself. Wrap it withing an anonymous function and you're good!

fnLikeIcon(colName, rowData) {
   let currentStatus = '';
   if (rowData.isLike) {
    currentStatus = `<span class="mdi mdi-ok" onclick=${() => this.changeFavouriteProperty(colName, rowData)}"></span>`;
   } else {
    currentStatus = `<span class="mdi mdi-outline" onclick=${() => this.changeLikeProperty(colName, rowData)}"></span>`;
   }
 return currentStatus;
}

Note: I'm not sure why you create your elements like this, I'm pretty sure there is a better way to do (using dynamic bindings on a vue template or a render function)

about 4 years ago · Juan Pablo Isaza Relatório
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