0

I'm trying to take a height given in feet and inches and convert it to inches (eg. 6'1" would be 73"). When I only have one select box (the feet box) I can successfully convert it to inches. But when I add the 'inches' variable and try to add it to the converted 'feet' variable nothing happens.

Here's the script:

<script type="text/javascript" language="javascript">

function Calculate() {

    var feet = document.getElementById("feet").value;
    var inches = document.getElementbyId("inches").value;

    height = ((feet*12) + inches);

    document.write ('This is your height in inches:<b>   ' + height.toFixed(0) + '</b><br>');
}


</script>

And the HTML:

<div class="form">
Height:
<select id="feet">
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
</select>

<select id="inches">
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
</select>
<br />
<input type="submit" value="Calculate" onclick="Calculate()" />

Thanks in advance

2
  • What's the type of a text box value? Commented Aug 23, 2012 at 0:14
  • 3
    You also have a typo in your code, it's getElementById with a capital B. Commented Aug 23, 2012 at 0:16

4 Answers 4

3

feet and inches are strings, so + concatenates. Multiplying casts feet to an integer, so your result for 6'1" would be 721 (72+"1")

You should convert to integers manually:

feet = parseInt(feet,10);
inches = parseInt(inches,10);

EDIT: Also, as João pointed out, you mis-capitalised getElementById on the second one.

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

1 Comment

Thanks! I figured it was something simple like that.
1

There are two problems.

First, in this line

var inches = document.getElementbyId("inches").value; 

You forgot to capitalize the b. It should be getElementById.

Also, inches comes from a textbox, which means it's a string, so

height = ((feet * 12) + inches);  

will also be a string because + is also a string concatenation operator. Since it's a string, it means that there is no toFixed method in its prototype.

You can fix this by passing the string to Number or prepending a + to make it a number, like this:

height = Number((feet * 12) + inches);
//OR
height = +((feet * 12) + inches);

Comments

0

you should be wrapping the the values of feet and inches in the parseInt function to ensure it is an integer.

When using the input type='submit' it has different behaviors than regular inputs with type='button' so you either need to change the type to button or put a return false at the end of your calculate function

Comments

0

I know this might be a little late, but I threw this into jsFiddle and cleaned up the JS code a little (I swapped it over to jQuery for kicks). It might be worth checking out.

example - jsFiddle

Comments

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.