0

I am trying to do 2-3 calculations at once with jQuery. I found a link to a fiddle that did one calculation and I edited it to what I thought I needed to make it all work, but I can only get the one-off calculation to work.

Basically, I'm calculating the overall width and height of tires based on the metric sizes.

Example: 265/70R17 The width is calculated by dividing the 3-digit width (265) by 25.4 = 10.43

The height is calculated by taking the width in inches, multiplying by the ratio (70), then doubling that number before adding the size of the rim (17). So, ((10.43 * .7) * 2) + 17 = 31.60

Here's the link to the fiddle that I've been trying to make work.
http://jsfiddle.net/fY57T/

$('#rim').change(function(ev){
    var twidth = $('#width').val() / 25.4;
    var step1height = $('#ratio').val() * twidth;
    var step2height = step1height * 2; 
    var theight = step2height + $('#rim').val(); 
  $('#theight').html((theight).toFixed(2));
  $('#twidth').html((twidth).toFixed(2));
});

Thanks in advance for any input anyone has.

1 Answer 1

1

the problem is with addition of step2height and rim id value, its considered as strings, not number. you need to use parseInt() to convert them to numbers

var theight = step2height + $('#rim').val();

Should be changed to

var theight = parseInt(step2height) + parseInt($('#rim').val());

Here is the updated Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for vote up. If it solves your problem, please accept the answer too.
Thanks for your help! I Feel I'm headed in the right direction. However, my last remaining issue is that only the width displays correctly (correct to the hundredths place). The height is rounding, so instead of displaying 31.62, it is showing up as 31.00. Here's my updated fiddle: jsfiddle.net/Cv2cj
ok as per requirement, you need to use parseFloat. Here is fiddle for you. jsfiddle.net/Cv2cj/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.