Solved

# Can any one provide me a javascript function to convert lb to Kg and Kg to lb

Posted on 2006-06-29
1,599 Views
Can any one provide me a javascript function to convert lb to Kg and Kg to lb. I have two fields one for lb and another for Kg. when I enter 152 in lb field and tab out
to kg field, kg field shows 68.95 and if tab out from kg field, lb field still should show 152, should not change it to 152.01
0
Question by:ukapu2005

LVL 6

Accepted Solution

inviser earned 125 total points
ID: 17013903
<script language="javascript">
function poundsToKilograms(lbs) {
return 0.4536*lbs;
}
</script>

0

LVL 6

Expert Comment

ID: 17013939
Here is something you might find useful:

<script language="javascript">
function poundsToKilograms(lbs) {
return 0.4536*lbs;
}
function kilogramsToPounds(kg) {
return kg/0.4536;
}
function lbsChanged(val) {
if (val.length > 0)
document.getElementById("kg").value = poundsToKilograms(val);
}
function kgChanged(val) {
if (val.length > 0)
document.getElementById("lbs").value = kilogramsToPounds(val);
}
</script>

Pounds: <input type="text" name="lbs" id="lbs" onkeyup="lbsChanged(this.value)" onkeydown="lbsChanged(this.value)"><br />
Kilograms: <input type="text" name="kg" id="kg" onkeyup="kgChanged(this.value)" onkeydown="kgChanged(this.value)">
0

LVL 28

Assisted Solution

Pravin Asar earned 125 total points
ID: 17014101
Here is a code, with
1. Input Validator .. permits 0-9 and . (decimal)
2. onblur (losing focus, updates the value, one function does it all.

<script language="javascript">
function onlyNumber(fld) {
if(fld.value.match(/[^0-9.]/)) {
fld.value=fld.value.replace(/[^0-9.]/g,'');
}
}
function convertUnit(sense, value) {
retValue = 0;
if (isNaN (value)) { alert ('Non-numeric value');  return 0; }
value = parseFloat (value);
var factor = 0.4536;
if (sense == 'kg2lb') {
retValue = value/factor;
}
else if (sense == 'lb2kg') {
retValue = value*factor;
}
return retValue;
}
</script>
<form>
<table>
<tr>
<td>
LB
<td>
<input type="text" name="lb" id="lb" onblur="this.form.kg.value=convertUnit('lb2kg',this.value);" onkeyup="onlyNumber(this);">
</tr>
<tr>
<td>
KG
<td>
<input type="text" name="kg" id="kg" onblur="this.form.lb.value=convertUnit('kg2lb',this.value);" onkeyup="onlyNumber(this);">
</tr>
</table>
</form>
0

LVL 28

Expert Comment

ID: 17014113
Use

var factor = 0.45359237;

for more precise conversion.
0

LVL 8

Assisted Solution

kaliyugkaarjun earned 125 total points
ID: 17017709
Insert into <BODY>

<script type="text/javascript" language="JavaScript">
<!--
//    Script Editor:   Howard Chen
//    Browser Compatible for the script: IE 3.0 or Higher
//                                       Netscape 2.0 or Higher
//    This script is free as long as you keep its credits
/*The way this works is the converter converts the number
into the smallest unit in the converter, in this case it will
be gram, and then it converts the unit fram gram to other units.*/
function nofocus()
{
document.convert.InUnit.focus()
}
var gValue = 1
var kgValue = 1000
var ounceValue = 28.3495
var lbValue = 453.592
var tValue = 907184
function toCM()
{
var i = document.convert.unit.selectedIndex
var thisUnit = document.convert.unit.options[i].value
if (thisUnit == "G")
{
document.convert.g.value = document.convert.InUnit.value
}
else if(thisUnit == "KG")
{
document.convert.g.value = document.convert.InUnit.value * kgValue
}
else if(thisUnit == "OUNCE" )
{
document.convert.g.value = document.convert.InUnit.value * ounceValue
}
else if(thisUnit == "LB" )
{
document.convert.g.value = document.convert.InUnit.value * lbValue
}
else if(thisUnit == "T" )
{
document.convert.g.value = document.convert.InUnit.value * tValue
}
toAll()
}
function toAll()
{
var m = document.convert.g.value
document.convert.kg.value = m / kgValue
document.convert.ounce.value = m / ounceValue
document.convert.lb.value = m / lbValue
document.convert.t.value = m / tValue
}
//-->
</script>
<center><h1>Weight Converter</h1></center>
<p> This script creates a weight converter that can convert five different units at one time. To use it, choose your unit at the first line and enter the a number in the box beside it, then click the convert button. It automatically converts your choice to all five units.</p>
<div align="center"><form name="convert">
<table border=1>
<tr><th>Unit</th><th>Number</th></tr>
<tr>
<td>
<select name="unit">
<option value = "G">Gram
<option value = "KG">Kilogram
<option value = "OUNCE">Ounce
<option value = "LB">Pound
<option value = "T">U.S. Ton
</select>
</td>
<td>
<input type="text" name="InUnit" size="20" maxlength="20" value="0">
</td>
</tr>
<tr>
<td>
Gram:                   </td>
<td>
<input type="text" name="g" size="20" maxlength="20" value="0" onFocus="nofocus()">
</td>
</tr>
<tr>
<td>
Kilogram:
</td>
<td>
<input type="text" name="kg" size="20" maxlength="20" value="0" onFocus="nofocus()">
</td>
</tr>
<tr>
<td>
Ounce:          </td>
<td>
<input type="text" name="ounce" size="20" maxlength="20" value="0" onFocus="nofocus()">
</td>
</tr>
<tr>
<td>
Pound:          </td>
<td>
<input type="text" name="lb" size="20" maxlength="20" value="0" onFocus="nofocus()">
</td>
</tr>
<tr>
<td>
U.S. Ton:               </td>
<td>
<input type="text" name="t" size="20" maxlength="20" value="0" onFocus="nofocus()"></td>
</tr>
<tr>
<td>
<input type="Reset" value="Start Over" width=100>
</td>
<td>
<input type="button" value="Convert" onClick="toCM()" width=150>
</td>
</tr>
</table>
</form>
</font>
</td></tr>
</table></div>

-------------End of body tag here
0

Assisted Solution

ElliotKatz earned 125 total points
ID: 17019045
I think you can do what you want to do by just using the onchange event so that nothing gets updated unless you changed the field you were in:
This worked for me in IE6:

<html>
<script language="javascript">
function lbsToKg(lbs) {return lbs*0.4536;}
function kgToLbs(kg) {return kg/0.4536;}
function setKg()
{
var lbs = document.forms["mainform"].elements["Pounds"].value;
var kg = lbsToKg(lbs);
document.forms["mainform"].elements["Kilograms"].value = kg;
}
function setLbs()
{
var kg = document.forms["mainform"].elements["Kilograms"].value;
var lbs = kgToLbs(kg);
document.forms["mainform"].elements["Pounds"].value = lbs;
}

</script>

<body>
<form name="mainform">
Pounds:
<input type="text" name="Pounds" onchange="setKg()"/>
<br>
Kilograms:
<input type="text" name="Kilograms" onchange="setLbs()"/>
</form>
</body>
</html>

0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

#### 744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!