Antonio
Antonio

Reputation: 15

jQuery calculation

I am new to jQuery and I would like to know how to accomplish a simple calculation based on the value selected in the picklist. please see the below screen shot I put on ImageShack for reference.

http://img707.imageshack.us/i/imagebg.png/ http://img707.imageshack.us/img707/5216/imagebg.png

I want to be able to show the result of the calculation in the text box at the right, If I enter number 2 on the text box on the left side and press Tab then the value of the text box on the right will update. If I can get to know how that works then I can take it from there. thank you very much in advance.

Upvotes: 0

Views: 491

Answers (4)

tvanfosson
tvanfosson

Reputation: 532445

Get the amount to convert from the text box (below named 'amount') and the conversion factor from the select list (below named 'scale') parsed as floating point numbers, then multiply the two and set that as the value of the conversion (below named 'converted').

$('#amount').change( function() {
    var amount = parseFloat( $(this).val() );
    var factor = parseFloat( $('#scale').val() );
    var converted = amount * factor;
    $('#converted').val( converted );
});

You'll need to do something similar for the select -- the first two lines change, but the rest can be shared.

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 72961

You will want to bind a blur event on your input field and a change event on your select field. These events should trigger a call to your calculate function that will read the values from each field, using val(), and perform the necessary calculation: inches to centimeter, inches to feet, etc.

If you post some of your HTML, I can add a code sample.

Upvotes: 0

James Curran
James Curran

Reputation: 103485

I should be something similar to this. You must provide the factors[] array.

$("#leftText").change(function()
{
   $("#rightText").val($("#leftText").val() * factors[$("#centerDDL").val()]);
});

Upvotes: 0

Matt Evanoff
Matt Evanoff

Reputation: 966

$(textbox1).change(function(event) {
  $(textbox2).val($(this).val() / 2.54);
})

Upvotes: 0

Related Questions