PDA

View Full Version : convert lbs to kg in table?

bphein1980
Feb 7th, 2005, 07:02 AM
I need help making a script...
Basically I will have a table with 2 columns....

<table>
<tr><th>Pounds</th><th>Kilograms</th></tr>
<tr><td>205</td><td> ?? </td></tr>
<tr><td>328</td><td> ?? </td></tr>
<tr><td>281</td><td> ?? </td></tr>
<tr><td>562</td><td> ?? </td></tr>
etc....
</table>

I am looking to have a script that will automatically take the pounds column and enter in the kilogram amount in the Kilograms column.

My Pounds column is being populated by an XML doc in this way....

<tr>
<td><span class="datacol" datafld="pounds"></span></td>
<td></td> <--- I want this to be the calculated Kilograms
</tr>

....
My XML doc is set this way....
<root>
<data dataID="1" pounds="205" />
<data dataID="2" pounds="328" />
<data dataID="3" pounds="281" />
etc....
</root>

.....
Is this possible? Im horrible with javascript, so the more specific help I can get, the better. Thanks for taking the time to look and possibly help!

jalarie
Feb 9th, 2005, 04:24 PM
I can't help with the xml part, but I can give two items that might be interesting:

1. lbs is a measurement of weight while kgs is a measurement of mass. It is improper to even consider a conversion without specifying other conditions that would result in a value for the gravitation factor.

2. With #1 in mind, there are 2.2046 lbs per kgs. To get kgs from lbs, divide by 2.2046.

bphein1980
Feb 9th, 2005, 10:22 PM

The reason I am trying to do with conversion is....

Its a website for my gym where I am keeping records of what the competitors have lifted. Mostly, I get the data in pounds, but there are many people that would prefer them to be in kilograms. Since it would be ALOT of work to convert each and every number into kilos by hand, I figured someone out there would know how to do this.

I think I got the XML part down, I can display all my data just fine...Im just looking for some kinda script that will convery the pounds column into Kilos and display right next to the pounds.

Anyone out there have any ideas on this?

Thanks,

shlagish
Feb 9th, 2005, 10:52 PM
I don't know how much you know about js, but this converts pounds to kilograms.

<html>
<title>Pounds to Kilos</title>
<script type="text/javascript">
<!--

function convert(){

var pounds=document.getElementById("pounds").value;
document.getElementById("kilos").value=pounds/2.2046;
}

-->
</script>
<body>

Weight in pounds:
<input type="text" id="pounds" />lbs
<input type="button" onclick="convert()" value="Convert">
<br />
Weight in kilos:
<input type="text" id="kilos" />kgs

</body>
</html>

Are you able to apply this to your situation?

bphein1980
Feb 9th, 2005, 11:24 PM

It would be similar to that, but instead of having to manually input a number to convert, I need it to be automatically grabbed from the html table.

<table border="1">
<col id="pounds" />
<col id="kilos" />

<tr>
<th>pounds</th>
<th>Kilos</th>
</tr>

<tbody>
<tr>
<td><td>200</td>
<td>I need a script that will automatically input the kilo value here</td>
</tr>
<tr>
<td><td>320</td>
<td>I need a script that will automatically input the kilo value here</td>
</tr>
</tbody>
</table>

Thanks!

shlagish
Feb 10th, 2005, 01:34 AM
Here is a first attempt at what you're trying to do. You can develop on this idea :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Shawn" />
<title>Pounds to Kilos</title>
<script type="text/javascript">
<!--

function convert(){

var tds=document.getElementsByTagName("td"),
i, j=0,
pounds=new Array(),
kilos=new Array();
for(i=0;i<tds.length;i++){
if(tds[i].className=="pounds"){
pounds[j]=tds[i];
j++;
}
}
j=0;
for(i=0;i<tds.length;i++){
if(tds[i].className=="kilos"){
kilos[j]=tds[i];
j++;
}
}
for(i=0;i<kilos.length;i++){
kilos[i].innerHTML=(pounds[i].innerHTML)/2.2046;
}
}

-->
</script>
<table border="1">
<tr>
<th>pounds</th>
<th>Kilos</th>
</tr>
<tr>
<td class="pounds">58</td>
<td class="kilos"></td>
</tr>
<tr>
<td class="pounds">320</td>
<td class="kilos"></td>
</tr>
</table>
</body>
</html>

shlagish
Feb 10th, 2005, 01:58 AM
very slightly improved:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Shawn" />
<title>Pounds to Kilos</title>
<script type="text/javascript">
<!--

function convert(){

var tds=document.getElementsByTagName("td"), i, j=0, k=0, pounds=[], kilos=[];
for(i=0;i<tds.length;i++){
if(tds[i].className=="pounds"){
pounds[j]=tds[i];
j++;
}
else if(tds[i].className=="kilos"){
kilos[k]=tds[i];
k++;
}
}
for(i=0;i<kilos.length;i++){
kilos[i].innerHTML=(pounds[i].innerHTML)/2.2046;
}
}

-->
</script>
<table border="1">
<tr>
<th>pounds</th>
<th>Kilos</th>
</tr>
<tr>
<td class="pounds">58</td>
<td class="kilos"></td>
</tr>
<tr>
<td class="pounds">50</td>
<td class="kilos"></td>
</tr>
</table>
</body>
</html>