Jump to content

[SOLVED] Auto-populate input field


ainoy31

Recommended Posts

Hello-

 

Scenario:

 

I have two input fields with two radio buttons.  The buttons will either convert the metric from lbs to kgs or stays as kgs base on what button is selected.  It is not converting from lbs -> kgs or stays as kgs.  Here is my code:

 

First is the input field:

<tr>

<td>Actual Weight:</td>

<td><input type="text" name="lbs" id="lbs" size="5">

<input type="radio" name="lbs" id="lbs" onClick="convert(); ">Lbs

<input type="radio" name="kgs" id="kgs" onClick="convert(); ">Kgs

= <input type="text" name="result" id="result" size="5">Kgs

</td>

</tr>

 

My Javascript to convert:

$(document).ready(function()

{

$('input.len, input.wid, input.hei, input.sum, input.lbs, input.kgs, input.sum1, input.sum2').keyup(function()

{

      convert()

});

});

 

function convert()

{

var kg = document.getElementById('kgs').value;

var pounds = document.getElementById('lbs').value;

if(kg == 0)

{

var result = pounds / 2.2;//gives the kgs from lbs

}

else

{

var result = kg;

}

sum_result(result);

}

function sum_result(result)

{

document.getElementById('result').value = result;

}

 

I have tried if(kg == null or kg == '') and no luck.  I am probably overseeing it.  Much appreciation.  AM

 

Link to comment
Share on other sites

try this:

 

<script language="javascript">

function convertUS()
{
var lbsvalue = document.getElementById('amount').value;
document.getElementById('result').value=""+lbsvalue+"";
if (lbsvalue.length < 1) {
document.getElementById('result').value="0";
}
document.getElementById('measurement').innerHTML=" Lbs";
}

function convertNonUS()
{
var nonusameasure = document.getElementById('weight2').value;
var lbsvalue = document.getElementById('amount').value;
document.getElementById("result").value = lbsvalue / 2.2;
document.getElementById('measurement').innerHTML=" Kgs";
}
</script>


<table>
<tr>
<td>Actual Weight:</td>
<td><input type="text" name="lbs" id="amount" size="5">
<input type="radio" name="weight" value="lbs" id="weight" onClick="convertUS(); ">Lbs
<input type="radio" name="weight2" value="kgs" id="weight2" onClick="convertNonUS(); ">Kgs
= <input type="text" name="result" id="result" size="5"><span id="measurement"></span>
</td>
</tr>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.