I have a form that allows the user to enter a weight, in either pounds or kilograms.
I’d like to store all of the data in kilograms, and using jQuery, would like the form to convert the weight_value to kilograms and change the weight_units option to “kg”.
1 pound = 0.45359237 kilograms
The function should round the weight_value to the nearest kilogram (no decimals).
Any help you could provide would be appreciated!
<label> Weight:</label>
<input type="text" id="weight_value" />
<select id="weight_unit" />
<option value="lbs">lbs</option>
<option value="kg">kg</option>
</select>
Here is a try:
See it live on: http://jsfiddle.net/zeByX/
[EDIT] With the
Math.floor(), you can get some weird results by switching from kg to pounds (try with 12 pounds and switch).