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

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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