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

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
ukapu2005Asked:
Who is Participating?
 
inviserConnect With a Mentor Commented:
<script language="javascript">
function poundsToKilograms(lbs) {
    return 0.4536*lbs;
}
</script>

0
 
inviserCommented:
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
 
Pravin AsarConnect With a Mentor Principal Systems EngineerCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Pravin AsarPrincipal Systems EngineerCommented:
Use

var factor = 0.45359237;

for more precise conversion.
0
 
kaliyugkaarjunConnect With a Mentor Commented:
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
 
ElliotKatzConnect With a Mentor Commented:
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>
<head>
<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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.