problem with parseint sum()

Asked

Viewed 79 times

2

I’m trying to add more "1" with parseint() to the number but it always returns me the original number with "1" next to it, for example 10 + 1 it returns me 101 (hit mizeravi). Note: I am using the split because in my full function I return to several different div’s different values.

 function teste(conteudo) {
      dados = conteudo.split('|');
      var adicicaoEstoque = parseInt(dados[1] + 1);
      document.getElementById(dados[0]).innerHTML = adicicaoEstoque;
 } 
<button onclick="teste('resultado|10')">adicione + 1</button>
<div id="resultado"></div>

1 answer

4


The correct would be to first convert the "10" and then add 1, otherwise it makes the operation "10" + "1" which gives "101".

Look at it this way:

var adicicaoEstoque = parseInt(dados[1]) + 1;

function teste(conteudo) {
    dados = conteudo.split('|');
    var adicicaoEstoque = parseInt(dados[1]) + 1;
    document.getElementById(dados[0]).innerHTML = adicicaoEstoque;
} 
<button onclick="teste('resultado|10')">adicione + 1</button>
<div id="resultado"></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.