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

147
Visualizações
How can i add number to a javascipt Span ID

I have an HTML webpage that gets a number from an external javascript using this:

<span id="numl"></span>

but I want to add the number 10 to the generated number, how can I do this, for example, the code above gets number "8" but instead of showing "8" it should add 10 to it which would be equals to "18"

I'm new to javascript and don't know how to do this

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The following code should increment the number by ten.

To resolve the timing issue you can use a Mutation Observer to change the number as soon as it is changed.

//source: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
// identify an element to observe
const elementToObserve = document.getElementById("num1");

//global variable to monitor changecount
window.changeCount = 0;

// create a new instance of `MutationObserver` named `observer`,
// passing it a callback function
const observer = new MutationObserver(function() {
    if(window.changeCount == 0) //so it wont recursively increment by 10
    {
      element = document.getElementById("num1");//get Element
      element.innerText = parseInt(element.innerText) + 10; //increment innerText by 10
    }
    window.changeCount = window.changeCount + 1; //increment change count by one

});

// call `observe()` on that MutationObserver instance,
// passing it the element to observe, and the options object
observer.observe(elementToObserve, {subtree: true, childList: true});

about 4 years ago · Juan Pablo Isaza Relatório
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