0

Following code instead of returning the addition results in appending the value of variable with the number. pl. help. thanks in advance.

  <!DOCTYPE html>
  <html>
  <head>
  <title></title>
  </head>
  <body>


  <input type="number" id="wd" value=1000>
  <br>
  Enter Height of chart
  <input type="number" id="ht" value=1000>
  <br>
  <body>
  <script type="text/javascript">
    var retWidth = document.getElementById('wd').value;
    var retHeight = document.getElementById('ht').value;
    wd1 = retWidth + 5; // hoping to get result 1005
    ht1 = retHeight + 5;
    console.log("wd1 = "+wd1)
    console.log("ht1 = "+ht1)
    </script>
  </body>

  </html>

  console log: 
  wd1 = 10005
  ht1 = 10005
1
  • var retWidth = +document.getElementById('wd').value; Commented Apr 4, 2016 at 10:52

1 Answer 1

1

Use parseInt() method to turn the strings back into numbers. Right now they are strings so adding two strings concatenates them, which is why you're getting 10005. Use parseInt(width)+5

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I was able to help. WC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.