4

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.......................

3
  • 1
    var sum=+document.getElementById('new').value; Commented Sep 1, 2015 at 5:36
  • var sum=parseInt(document.getElementById('new').value); Commented Sep 1, 2015 at 5:54
  • It is because of it is treating sum value as a string so when you are trying to add it is just concatenating the values. Convert the value into Number and then try to add .Then It will give the expected outpurt Commented Sep 1, 2015 at 6:02

9 Answers 9

3

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' />

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

Comments

2

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;
}

Comments

0

DO parseInt()

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

Comments

0

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;
}

Comments

0

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);

Comments

0

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

Comments

0

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

Comments

0

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>

Comments

0

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

1 Comment

Do you mean var sum = parseFloat( document.getElementById('new').value );

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.