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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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