Much better you take the amount with textContent
instead of innerHTML
. This is because if you solve then add some other tag in the element’s HTML, it will again return Nan.
Examples with innerHTML
:
✓ OK! IRÁ RETORNAR 0
<p id="numero">0</p>
parseInt(document.getElementById('numero').innerHTML);
X ERRO! IRÁ RETORNAR NaN
<p id="numero"><b>0</b></p>
parseInt(document.getElementById('numero').innerHTML);
Testing:
function aparecer() {
const elemento1 = document.getElementById('numero').innerHTML;
const elemento2 = parseInt(elemento1);
alert(elemento2)
}
<button onClick="aparecer()"> Aparecer </button>
<button onClick="desaparecer()"> Desaparecer </button>
<p id="numero"><b>0</b> </p>
Examples with textContent
:
✓ OK! IRÁ RETORNAR 0
<p id="numero">0</p>
parseInt(document.getElementById('numero').textContent);
✓ OK! IRÁ RETORNAR 0
<p id="numero"><b>0</b></p>
parseInt(document.getElementById('numero').textContent);
Testing:
function aparecer() {
const elemento1 = document.getElementById('numero').textContent;
const elemento2 = parseInt(elemento1);
alert(elemento2)
}
<button onClick="aparecer()"> Aparecer </button>
<button onClick="desaparecer()"> Desaparecer </button>
<p id="numero"><b>0</b> </p>
The innerHTML
has function to manage HTML code and not to pick text or values in text form.
value
exists, but only in the elements that extend from Htmlinputelement and some other elements of<form>
– Valdeir Psr
I meant in case the TAG
<p>
in itself did not possess the attributeVALUE
then it does not exist for the elements.– RpgBoss