Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
Access variable from MutationObserver callback globally

I have a div that is getting its inner text changed by another element. In my js file I have the following, which on DOM ready state, starting monitoring the target for mutations. If I put, for example, console.log() of any of the vars inside changeToVar function, it behaves as expected and accesses the variables. What I want though is to be able to access the MON_DATA and CONT_DATA outside of the changeToVar function.

document.addEventListener("DOMContentLoaded", function (event) {
  var target = document.getElementById("recidRecord");
  var observer = new MutationObserver(function (mutations) {
    changeToVar(target.innerText);
  });

  observer.observe(target, {
    attributes: true,
    childList: true,
    characterData: true,
  });
});

function changeToVar(target) {
  window.data = target;
  var CONST_DATA = CONST_DETAILS[window.data];
  window.MON_DATA = CONST_DATA.monitored_props;
  window.CONT_DATA = CONST_DATA.contingencies;
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda