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

249
Visualizações
Javascript, alert when value is zero

I am designing a game.

There is a life point, id called 'lifepoint'.

Also, there is a function: mouse click to decrease the value of 'lifepoint'.

I want to have new function:

Alert when the lifepoint <= 0, my alert function is not work.

function decrementlife() {
    var element = document.getElementById('lifepoint');
    var value = element.innerHTML;

    --value;
    console.log(value);
    document.getElementById('lifepoint').innerHTML = value;
}




$(function (){
    var value = $('lifepoint').val();
    if( value  == '0' ) {
        alert('GG')
    }
})

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Why not directly put inside the decrementlife function ?

function decrementlife() {
    var element = document.getElementById('lifepoint');
    var value = parseInt(element.innerHTML);// it's a string with innerHTML so convert to int

    --value; 
    if(value <= 0) { alert("Boom!!")} // alert it here
    console.log(value);
    document.getElementById('lifepoint').innerHTML = value;
    
}

about 4 years ago · Santiago Trujillo Relatório

0

$(function (){}) is called once all the DOM elements of the page are ready to be used.

your should call alert directly in decrementlife

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