jquery - How to sum two input fields -


i have following layout in html table:

<table> <tr><td>id</td><td>item name</td><td>value</td></tr> <tr><td>1</td><td>shampoo</td><td>200</td></tr> <tr><td>2</td><td>soap</td><td>20</td></tr> <tr><td>3</td><td>toothpaste</td><td>8</td></tr> <tr><td></td><td>others</td><td><input class="ta" type="text" value="" /></td></tr> <tr><td></td><td>total</td><td><input id="tp" type="text" value="228" /></td></tr> </table> 

data of table rows fetched mysql database except 5th row (item:others). while page loads, value of "total" fetched mysql database. if user put value "others" row, everytime value of "total" should sum user input value + value of "total" row.

i trying following jquery code:

$(document).ready(function(){ $(".ta").keyup(function(){   var vat = $(this).val();     var total = $("#tp").val();         total = vat+total;     $("#tp").val(total); }); }); 

however, not doing sum of 2 value, instead adding input value. i,g: if total 5,000 , user input 50, can't calculate total 5050, instead changing total value 505000. think of these 2 fields evaluating text instead digit jquery.

what should right code?

you need parse textboxes values before adding them. need modify input element selector. ta id of element. should use id selector(#):

$("#ta").keyup(function(){ var vat = parseint($(this).val()); var total = parseint($("#tp").val());     total = vat+total; $("#tp").val(total); }); 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -