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

95
Vistas
Calling javascript functions from html code that I inject

I have written a javascript and injecting it via a chrome extension such as Tamper Monkey. I find all the tags and then change it to a button but I am not able to make the click work. My page has some text called "Approver". I replace that with a button with the same text "Approver" and it shows as a button, but when I click it, the function ptmTest is not getting called. Below is part of the Javascript code that I am using to try and make this work. Any help on what I am doing wrong is greatly appreciated.

function ptmButton() {
  var x         = document.querySelectorAll( "dt" );
  var ptmButton = `<button class="aui-buttons trigger-label" onClick="ptmTest()">Approver</button>`
  var i;
  for ( i = 0; i < x.length; i++ ) {
    if ( x[i].innerText == "Approver" ) {
      x[i].innerHTML = ptmButton;
    }
  }

  function ptmTest() {
    console.log( "PTM clicked" )
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are 2 solutions:

you can put the function ptmTest outside ptmButton:

function ptmButton() {
    var x = document.querySelectorAll( "dt" );
    var ptmButton = `<button class="aui-buttons trigger-label">Approver</button>`
    var i;
    for ( i = 0; i < x.length; i++ ) {
        if ( x[i].innerText == "Approver" ) {

            x[i].innerHTML =
                ptmButton;
        }
    }

}

function ptmTest() {
    console.log( "PTM clicked" )
}

you can also set the onClick in the script:

function ptmButton() {
    var x = document.querySelectorAll( "dt" );
    var ptmButton = `<button class="aui-buttons trigger-label" onClick="ptmTest()">Approver</button>`
    var i;
    for ( i = 0; i < x.length; i++ ) {
        if ( x[i].innerText == "Approver" ) {

            x[i].innerHTML =
                ptmButton;
            x[i].children[0].onclick = (e)=> {
                ptmTest();
            }
        }
    }

    function ptmTest() {
        console.log( "PTM clicked" )
    }
}
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