Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
    }
})

>Solution :

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!!")}**
    console.log(value);
    document.getElementById('lifepoint').innerHTML = value;
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading