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

290
Vistas
How can I trigger a function when certain text on a specific element appears?

I need to add some code to a webpage when a certain text appears on it.

So I load a webpage, and this appears:

<div id="someid" class="someclass" style=""></div>

I don't want anything to happen. But at some point this appears:

<div id="someid" class="someclass" style="">Some text here...</div>

When the text appears, I want Greasemonkey to add this somewhere on the page.

<div id="idbla">This is text...</div>

I don't care where this is added. It doesn't need to be appended to anything. It doesn't even need to be visible on the page. I just want it to appear somewhere in the HTML of the webpage.

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

0

Use a MutationObserver to detect text changes to your element:

function main() {
  console.log('Text added!');
}

const wantedDiv = document.querySelector('#someid');
const button = document.querySelector('#add-text');
button.addEventListener('click', () => wantedDiv.innerText = 'text...');

const observer = new MutationObserver(() => {
  if (wantedDiv.innerText) main();
  observer.disconnect();
});
const config = {
  characterData: false,
  attributes: false,
  childList: true,
  subtree: false
};

observer.observe(wantedDiv, config);
<div id="someid" class="someclass" style=""></div>

<button id="add-text">Add text</button>

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