i cant += value

moneygetterhaha.value += 1;
9 Replies
NIMA
NIMAOP9mo ago
why i cant += input value but i can do this moneygetterhaha.value -= 1 ; moneygetterhaha.value *= 2 ; moneygetterhaha.value /= 2 ; how can i fix the moneygetterhaha.value += 1;
MarkBoots
MarkBoots9mo ago
because the + sign also is a way to concatenate strings. as the value of an input is a string, it will append the 1, not add it up. these options should work - moneygetterhaha.value = Number(moneygetterhaha.value) + 1 - moneygetterhaha.value = moneygetterhaha.valueAsNumber + 1 - moneygetterhaha.value = +moneygetterhaha.value + 1 - moneygetterhaha.value++ (only works with adding up 1)
NIMA
NIMAOP9mo ago
tnx so much it work i stuck in it for 1 hours
MarkBoots
MarkBoots9mo ago
the other operations don't have double functions, so JS knows it has to be converted to a number
NIMA
NIMAOP9mo ago
so only for + this happen
MarkBoots
MarkBoots9mo ago
indeed
glutonium
glutonium9mo ago
it's just better if u do .valueAsNumber u won't have to face all these issues
MarkBoots
MarkBoots9mo ago
you can't update the value of an input with valueAsNumber directly.
glutonium
glutonium9mo ago
ooh dang.. makes sense tnx

Did you find this page helpful?