Jump to content

[SOLVED] Input field calculation


ainoy31

Recommended Posts

Here is my scenario:

 

I have four input fields and the first three is asking for the dimension in length, width, and height.  The fourth input field will store the final calculation. 

 

<tr>

<td>Dimensional inches to weight(kgs)</td>

<td>

<input type="text" name="len"  class="len" size="3">

<input type="text" name="wid"  class="wid" size="3">

<input type="text" name="hgt"  class="hgt" size="3">

<input type="text" name="sum"  class="sum" size="3">

</td>

</tr>

 

Then I have my javascript as:

 

<script type="text/javascript">

$(document).ready(function()

{

$('.datepicker').datePicker();

$('input.len, input.wid, input.hei, input.sum').keyup(function()

{

calc_Dim_wgt();

});

});

 

function calc_Dim_wgt()

{

len = $(this).find('input.len');

wid = $(this).find('input.wid');

hgt = $(this).find('input.hgt');

sum = $(this).find('input.sum');

sum = len * wid * hgt;

}

</script>

 

Any suggestion is much appreciated.  AM

Link to comment
https://forums.phpfreaks.com/topic/79611-solved-input-field-calculation/
Share on other sites

OK.  I am almost there.  I am able to do the calculation after filling in the three fields but I still have to click on the fourth field to give me the total.  I want to display the total after filling in the third field.  Here is my code:

 

<script type="text/javascript">

$(document).ready(function()

{

$('.datepicker').datePicker();

$('input.len, input.wid, input.hei, input.sum').keyup(function()

{

calculate_sum()

});

});

 

function calculate_sum()

{

var length = document.getElementById('len').value;

var width = document.getElementById('wid').value;

var height = document.getElementById('hgt').value;

var sum = length * width * height;

sum_result(sum);

}

function sum_result(sum)

{

document.getElementById('sum').value = sum;

}

</script>

 

Input fields:

 

<input type="text" name="len"  id="len" size="3">

<input type="text" name="wid"  id="wid" size="3">

<input type="text" name="hgt"  id="hgt" size="3">

<input type="text" name="sum"  id="sum" size="3" onClick="calculate_sum();">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.