Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda