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

85
Vistas
Pass function as argument to another function called inside the first one

I have a function that creates a button with an "onclick" action that calls a second function. This second function will recieve parameters of the first one (includind a function). Just like this:

function funcOne(argFunc, customMsg) {
    let div = document.createElement('div');
    let button = `<button onclick='funcTwo(`+argFunc+`','`+customMsg+`)'>Go!</button>`;
    div.innerHTML = button;
    document.body.appendChild(div);
}

function funcTwo(argFunc, customMsg) {
    argFunc(customMsg);
}

Then, calling funcOne:

funcOne(function(customContent) { alert(customContent) },
'Hello World');

I get "Uncaught SyntaxError: Unexpected end of input" error here:

let button = <button onclick='funcTwo(+argFunc+','+customMsg+)'>Go!</button>;

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

0

In general, avoid onxyz-attribute-style event handlers. Always avoid them when you have a function (rather than string) you need to hook up dynamically. They have several issues, not least that they can only call global functions.

Instead, use modern event handling. There's also no need for HTML parsing:

function funcOne(argFunc, customMsg) {
    const div = document.createElement("div");
    const button = document.createElement("button");
    button.textContent = "Go";
    button.addEventListener("click", () => {
        funcTwo(argFunc, customMsg);
    });
    div.appendChild(button);
    document.body.appendChild(div);
}

function funcTwo(argFunc, customMsg) {
    argFunc(customMsg);
}
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