Solved

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

Posted on 2006-06-29
8
1,657 Views
Last Modified: 2012-06-21
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
Comment
Question by:ukapu2005
8 Comments
 
LVL 6

Accepted Solution

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

0
 
LVL 6

Expert Comment

by:inviser
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

by:Pravin Asar
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:Pravin Asar
ID: 17014113
Use

var factor = 0.45359237;

for more precise conversion.
0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
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

by:ElliotKatz
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>
<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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now