1

I want to access the values entered in the input box and then get their sum, but javascript is concatenating the result. Here is the code

<script>

   $(document).ready(function(){
     $( "#dateSelector" ).click(function(){ 
    $(this).datepicker();
     });
   });

  $(document).ready(function(){
    $("#ocb750cb").click(function(){
      var closingBal = 0;
    var openingBal = document.getElementById("ocb750ob").value;
    alert(openingBal);
    Number(openingBal);
    var stockBal =  document.getElementById("ocb750sb").value;
    alert(stockBal);
    Number(stockBal);
    var sold = document.getElementById("ocb750sl").value; 
    alert(sold);
    var store = document.getElementById("ocb750cb");
        store.value = (stockBal + openingBal) - sold;
    });



 });



</script>



<th class = "main brand">OCB 750</th>
<th class = "main"><input type="text" maxlength="5" id="ocb750ob"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750sb"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750tl"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750sl"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750rs"></th>
<th class = "main"><input type="text" maxlength="5" id="ocb750cb"></th>

suppose i enter 5 and 2 my output will be 52 and not 7, it is getting concatenated

Swaroop Nagendra
  • 609
  • 2
  • 15
  • 25
  • `value` returns a string, `+` concatenates strings. `+value` should do. – elclanrs May 29 '13 at 09:31
  • Since *all* of the answers so far managed to get this wrong, I 'll say it here: **you have to specify the radix (second argument) when using `parseInt`**. See http://stackoverflow.com/questions/6611824/why-do-we-need-to-use-radix – Jon May 29 '13 at 09:37
  • You should really check out http://try.jquery.com. Also some of your HTML isn't valid. `` should be ``. Equally, you probably don't need to put that class on all of your `th` elements. – James Donnelly May 29 '13 at 09:38

6 Answers6

0

You values have string format,you need use parceInt

vborutenko
  • 4,323
  • 5
  • 28
  • 48
0

The value of a textbox is text. You need to cast it to a number:

var openingBal = parseFloat(document.getElementById("ocb750ob").value);
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0

You have to use parseInt(x) to be sure, that value is interpreted as integer. Otherwise javascript interpret it as string, so + operator is concatenating.

Use:

 var openingBal = parseInt(document.getElementById("ocb750ob").value);

Etc.

Michal Politzer
  • 313
  • 3
  • 9
0
    var stockBal =  document.getElementById("ocb750sb").value;

stockbal will be a string, so you have to parse it to get an integer

   try{
      var stockBalInt = parseInt(stockBal);
   }catch(error){
    -..handle error
   }

Since you concatenate two strings together, "5" + "2" = "52"

rawphl
  • 331
  • 2
  • 8
0

You should use parseInt():

store.value = ( parseInt(stockBal) + parseInt(openingBal) ) - parseInt(sold);
RRikesh
  • 14,112
  • 5
  • 49
  • 70
0

I think it's because all the variables retrieved using getElementById("id").value are "string". Try to do like this:

var sold = parseInt(document.getElementById("id").value);

Hope this will help.

Jiji TANG
  • 91
  • 3