0

I'm learning JavaScript and I wanted to accept user input and add a number to it ,but instead of outputting 12+12 = 24 it's outputting 12+ 12 = 1212.

document.getElementById("mybutton").onclick = function(){
  var myAge = document.getElementById("mytext").value + 12;
  document.getElementById("value").innerHTML = myAge;
}

I tried doing: myName = Number(myName) but it didn't work

j08691
  • 204,283
  • 31
  • 260
  • 272
John
  • 25
  • 1
  • 3
  • 4
    Please research your inquiry before posting; duplicate questions aren't permitted here in the interest of content quality. Duplicate of [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – esqew Feb 20 '22 at 18:50
  • I saw that post and it didn't help. that is different – John Feb 20 '22 at 18:58

1 Answers1

0

In this case use a function parseInt() example:

document.getElementById("mybutton").onclick = function(){
  var myAge = parseInt(document.getElementById("mytext").value) + 12;
  document.getElementById("value").innerHTML = myAge;
}