Ghanshyamji Gupta
Ghanshyamji Gupta

Reputation: 429

Add two variable in javascript having numeric value

I am making a program to add 10% in a value entered by user with java script.

my HTML code

<input id='new' onKeyUp='doit()' name='hello' onkeydown='doit()' value=''  placeholder='Type a numeric value' />
<input id='new1' value=''  placeholder='final value' />

my java script code

<script>
function doit()
{
var sum=document.getElementById('new').value;
var extra=sum/10;
var newamount=sum + extra;
document.getElementById('new1').value=newamount;
}
</script>

js.do output

on giving input 100 expected output is 110 but actual output comming is 10010

Although i am adding according to rule.... w3schools example

Please tell me where i am wrong.......................

Upvotes: 4

Views: 6786

Answers (9)

Ian Yockey
Ian Yockey

Reputation: 1

and if you are using numbers like 57.435 you can use parseFloat(sum)

Upvotes: 0

Hope
Hope

Reputation: 644

Use parseFloat() instead of parseInt() because parseInt only output whole number. Try this...

<input id='new' onKeyUp='doit()' name='hello' onkeydown='doit()' value=''  placeholder='Type a numeric value' />
<input id='new1' value=''  placeholder='final value' />
<script>
function doit()
{
var sum=parseFloat(document.getElementById('new').value);
var extra=sum/10;
var newamount=sum + extra;
document.getElementById('new1').value=newamount;
}
</script>

Upvotes: 0

Joy Biswas
Joy Biswas

Reputation: 6527

Actually the document.getElementById('input_element').value will return a string. In a mathematical expression in JS operations such as / * - will be fine eg Mathematical but the + actually concatenates string for c = a + b if any of the a or b is a string c will be the concatenated value else if both a and b are numbers c will be addition of both numbers . To avoid either do a parseInt() or multiply by 1 before adding to another number. I have added a *1 to var newamount=sum*1 + extra; You can also use var newamount=parseInt(sum) + extra;

Snippet Added below:

function doit()
{
  var sum=document.getElementById('new').value;
  var extra=sum/10;
  var newamount=sum*1 + extra;
  document.getElementById('new1').value=newamount;
}
<input id='new' onKeyUp='doit()' name='hello' onkeydown='doit()' value=''  placeholder='Type a numeric value' />
<input id='new1' value=''  placeholder='final value' />

Upvotes: 3

Mukesh Singh Rathaur
Mukesh Singh Rathaur

Reputation: 13105

document.getElementById('textId').value

Return A String, representing the value of the text field but not the Fixnum/Numeric type. So before you apply any arithmetic you have to convert it to numeric type.

More details pls refer Input Text value Property on w3schools

Upvotes: 0

Rajesh kannan
Rajesh kannan

Reputation: 634

use parseIntin Javascript.

I'm re writing your code with parseInt.

function doit()
{
var sum=document.getElementById('new').value;
var extra=parseInt(sum)/10;
var newamount=parseInt(sum) + parseInt(extra);
document.getElementById('new1').value=newamount;
}

Let me know if it is helpful

Upvotes: 0

Moishe Lipsker
Moishe Lipsker

Reputation: 3034

Use the parseInt function to turn sum into a number so that sum + extra will not concatenate the two, but will add them numerically.

var sum = parseInt(document.getElementById('new').value);

Upvotes: 0

venkat7668
venkat7668

Reputation: 2767

use parseInt js.do

function doit()
{
   var sum=document.getElementById('new').value;
   var extra=sum/10;
   var newamount=parseInt(sum) + extra;
   document.getElementById('new1').value=newamount;
}

Upvotes: 0

sajanyamaha
sajanyamaha

Reputation: 3198

DO parseInt()

var newamount=parseInt(sum) + parseInt(extra);

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

Try this:

function doit()
{
var sum=document.getElementById('new').value;
var extra=parseInt(sum)/10;
var newamount=parseInt(sum) + extra;
document.getElementById('new1').value=newamount;
}

Upvotes: 2

Related Questions