-2

How to convert "string" in to "int" ?

This code is giving me the result in "string" format only but i need in "int" format so that i can perform further calculation.

function myFunction() {
  var x = document.getElementById("myForm").elements[0].value;
  var y = document.getElementById("myForm").elements[1].value;
  var z = x + y;
  document.getElementById("demo").innerHTML = z;
}
<form id="myForm" action="/action_page.php">
  First name: <input type="number" name="fname" value="0"><br> Last name: <input type="number" name="lname" value="9"><br>
</form>

<button onclick="myFunction()">Submit</button>

<p id="demo" style="border: solid black 1px; width: 200px; height: 30px">Your Result Will Show Here
</p>
Daweed
  • 1,419
  • 1
  • 9
  • 24
  • 1
    Harshit, I realize you may be new to programming, but I would _strongly_ suggest you google your question, first. That will give you an answer faster than the time it takes to submit a question here, especially on question like this. – Cerbrus Apr 20 '21 at 10:10

1 Answers1

-1

update to the below:

var x = parseInt(document.getElementById("myForm").elements[0].value);
var y = parseInt(document.getElementById("myForm").elements[1].value);
Vasudev Bhat
  • 199
  • 5
  • Thanks a lot now its working.. and yes i am new to programming. :) i did google also but i found answer to add (.typeas). and in python there is different method like a = 12.5 b = int(a) to convert. something like that. – Harshit Aranya Apr 20 '21 at 10:30