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
https://forums.phpfreaks.com/topic/80019-solved-auto-populate-input-field/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.