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

102
Vistas
MutationObserver doesn't trigger when text changes

I have a simple tag on my html page

<div id="myid">My text</div>

Im new to JavaScript. I need that MutationObserver detect when text changes in "My text" here is my JavaScript code


var target = document.querySelector("#myid");

var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        console.log(mutation.type);
    });
});

var config = { 
    childList: true, 
    attributes: true, 
    characterData: true, 
    subtree: true, 
    attributeFilter: ["myid"], 
    attributeOldValue: true, 
    characterDataOldValue: true 
}

observer.observe(target, config);

observer.disconnect();
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you want to detect changes of a text field you can simply use onChange event, for example:

<input type="text" id="myid"/>

<script>
    let sel = document.getElementById('myid');
    sel.addEventListener ("change", function (data) {
       console.log(data.currentTarget.value);
    });
</script>
    

MutationObserver is good to use when you want to subscribe on DOM changes - like attribute changes etc. That's why youк code doesn`t work, because you don't have any changes related to DOM structure. See example with additionally adding new attribute:

const targetNode = document.getElementById('myid');

// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };

// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
    // Use traditional 'for loops' for IE 11
    for(const mutation of mutationsList) {
        if (mutation.type === 'childList') {
            console.log('A child node has been added or removed.');
        }
        else if (mutation.type === 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
};

// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);

//MODIFY THE ATTIBUTE TO SEE THE CHANGE
targetNode.style["color"] = "red";

If you want to play with it, please see: https://jsfiddle.net/a0vdxt5n/40/

about 4 years ago · Santiago Trujillo 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