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

126
Vistas
Javascript function inside polymer is not a function error

I have a javascript function defined in polymer as such: myTest(arg) {<do stuff here>}

Inside this function, based on some conditions, I want to change the onclick of a button defined in the polymer template to some other function. For this I'm doing document.getElementById('myButton').onclick = function() {myTest('someArg')};

The problem I'm having is that when the above line gets invoked I'm getting an error

Uncaught TypeError: myTest is not a function.

If I try to use the function keyword when declaring the function the compiler complains with

Unexpected token. A constructor, method, accessor, or property was expected.

What's the correct syntax to for changing the button's onclick function?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's hard to say for sure without seeing the the full source code example, but it sounds like the problem you're facing is related to JavaScript not inferring this as the context for calling instance methods in the same way that e.g. Java does. To make things more complicated, this wouldn't have the expected value inside the function that you create for the click listener.

One way around that would be to use an arrow function since they aren't redefining the execution context:

document.getElementById('myButton').onclick = () => { this.myTest('someArg') };

Another alternative is to explicitly capture this into a separate variable and then use that variable inside the function:

var self = this;
document.getElementById('myButton').onclick = function() { self.myTest('someArg') };
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