Problem with if () with variables that increase and change according to the database

1

I'm making a website for my mobile app, the truth is that I do not know much about Javascript (nothing really) and I have almost everything I need, the only thing that is giving me problems is this code if (), the which apparently does not recognize the variables because I do not execute the code that I have inside it if (), so if I use that same code outside of the if () it works perfectly, that's why I think it should be a problem of the variables inside the if ().

This is the famous if ()

function btnmasAClick() {
    var numero = parseInt(document.getElementById('number').value, 10);
    numero = isNaN(numero) ? 0 : numero;
    numero++;
    document.getElementById('number').value = numero;
    var postElement4 = document.getElementById("postElement4");
    if (numero == postElement4) {  
    //hago algo
    }
    var updateStarCount4 = function(element4, value) {
    element4.textContent = value;
    };
    var starCountRef4 = firebase.database().ref().child("user").child("lista-user").child(numero+'').child("Edad");
    starCountRef4.on('value', function(snapshot) {
    updateStarCount4(postElement4, snapshot.val());
    });
}

So I show the data received from the Firebase Database in the HTML code

<td id="postElement4"></td>

If you could help me it would be great, because of Javascript I do not really know much ... Of course, thank you very much for your time!

    
asked by Matías Nicolás Núñez Rivas 03.04.2018 в 16:03
source

1 answer

2

You are comparing an element with a number, since postElement4 is not getting the value of the label if you do not label it as such, so it should work for you:

function btnmasAClick() {
    var numero = parseInt(document.getElementById('number').value, 10);
    numero = isNaN(numero) ? 0 : numero;
    numero++;
    document.getElementById('number').value = numero;
    var postElement4 = parseInt(document.getElementById("postElement4").innerHTML,10);
    if (numero == postElement4) {  
    //hago algo
    }
    var updateStarCount4 = function(element4, value) {
    element4.textContent = value;
    };
    var starCountRef4 = firebase.database().ref().child("user").child("lista-user").child(numero+'').child("Edad");
    starCountRef4.on('value', function(snapshot) {
    updateStarCount4(postElement4, snapshot.val());
    });
}

With that you get the value within the element so you can compare it.

Did it work?

Is the problem still?

Change something after doing this?

Source:
Mozilla Developers. (October 23,2012). element.innerHTML . April 3,2018, from MDN Website: link

    
answered by 03.04.2018 / 16:16
source