Problem when converting units

Page 1 of 1

1 Replies - 582 Views - Last Post: 12 January 2013 - 10:27 AM

#1 christine_rose

Reputation: 0
• Posts: 1
• Joined: 12-January 13

Problem when converting units

Posted 12 January 2013 - 09:57 AM

My problem is that when I try to convert a unit, the result does not is displayed in the textbox

this is my code

<script type="text/javascript">

function convertVals() {
// retrieve values and types of and for conversion
var sel1_val1 = parseFloat(document.sel1.conv[sel1.conv.selectedIndex].value)
var sel1_val2 = document.sel1.conv[sel1.conv.selectedIndex].value2.toLowerCase()
var sel2_val1 = parseFloat(document.sel2.conv[sel2.conv.selectedIndex].value)
var sel2_val2 = document.sel2.conv[sel2.conv.selectedIndex].value2.toLowerCase()
var toConvert = parseFloat(document.getElementById('amt').value)

var display = ""

if(sel1_val2 == sel2_val2 && sel1_val2 != "sep" && sel1_val2 != "temp" && toConvert == parseFloat(toConvert)) {
// if same type, convert value
display = ((toConvert * sel1_val1) / sel2_val1).toFixed(8)
}
else if(sel1_val2 == sel2_val2 && sel1_val2 == "temp" && toConvert == parseFloat(toConvert)) {
if(sel1_val1 == 100 && sel2_val1 == 212) {
display = ((toConvert * 9 / 5) + 32).toFixed(8)
}
else if(sel1_val1 == 212 && sel2_val1 == 100) {
display = ((toConvert - 32) * 5 / 9).toFixed(8)
}
else {
display = toConvert.toFixed(8)
}
}
while(display.lastIndexOf('0') == display.length - 1 && display.length > 0) {
display = display.substr(0,display.length - 1)
}
if(display.lastIndexOf('.') == display.length - 1) {
display = display.substr(0,display.length - 1)
}
document.getElementById('amt2').value = display

if(display != "") {
for(var i=0;i<weights.length;i++) {
if(sel1_val1 == weights[i] && sel1_val2 == types[i].toLowerCase()) {
var firstNum = i
break;
}
}

for(var i=0;i<weights.length;i++) {
if(sel2_val1 == weights[i] && sel2_val2 == types[i].toLowerCase()) {
var secondNum = i
break;
}
}

history1[history1.length] = toConvert
history2[history2.length] = display
history3[history3.length] = secondNum
history4[history4.length] = firstNum
total = history1.length - 1
}
}

var types = new Array
var weights = new Array
var values = new Array

function setUpBox() {

values = new Array("Select Unity","-----------------","millimeter","centimeters","inches","decimeters","meters","feet","feet","hectometers","kilometers","Miles","-----------------","Square miles ","Square kilometers","-----------------","Dolar","cents","-----------------","Grams","ounces","pounds","Tons","kilograms","-----------------","Celcius","Fahrenheit")

weights = new Array(0,0,.001,.01,.0254,.1,1,.3048,10,100,1000,1609,0,0.386,1,0,1,.01,0,.00205,0.0625,1,2000,2.205,0,100,212)

types = new Array("sep","sep","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","metricWeight","sep","sqrUnits","sqrUnits","sep","currency","currency","sep","weight","weight","weight","weight","weight","sep","temp","temp")

var resultStr = '<select name="conv" size=5 onchange="convertVals()">'
resultStr += '<option value="'+ weights[0] +'" value2="'+ types[0] +'" selected>'+ values[0]

for(var i=1;i<values.length;i++) {
resultStr += '<option value="'+ weights[i] +'" value2="'+ types[i] +'">'+ values[i]
}

resultStr += '</select>'
document.write(resultStr)
}

var history1 = new Array
var history2 = new Array
var history3 = new Array
var history4 = new Array
var total = history1.length

function navHistory(num) {
if(history1[total - num] != undefined) {
document.getElementById('amt').value = history1[total - num]
document.getElementById('amt2').value = history2[total - num]
document.sel2.conv.selectedIndex = history3[total - num]
document.sel1.conv.selectedIndex = history4[total - num]
total -= num
}
}

function clearHist() {
var temp = history1.splice(0,history1.length-1)
var temp2 = history2.splice(0,history2.length-1)
var temp3 = history3.splice(0,history3.length-1)
var temp4 = history4.splice(0,history4.length-1)
total = 0
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body {
margin-top: 0px;
}
-->

<body><center class="Title">
</big></b>
</center><BR>
<table VALIGN="top" align="center" border="1" bordercolor="black" cellpadding=8 cellspacing=0>
<tr><td colspan="2"><center>
<span >Type in the amount to convert:</span>
<input type="text" id="amt" onclick="if(this.value==history1[history1.length-1]) this.value='';">
</center></td></tr>
<tr>
<td><center>
<form name="sel1">
<span >Unit: </span><BR>
<BR>
<script type="text/javascript"> setUpBox() </script>
</form>
</center></td>
<td><center>
<form name="sel2">
<span>To:</span><BR>
<BR>
<script type="text/javascript"> setUpBox() </script>
</form>
</center></td>
</tr>
<tr><td colspan="2"><center>
<input type="button" value="Convert >>" onclick="document.getElementById('amt').focus();convertVals()"> <input type="text" id="amt2">
</center></td></tr>
<tr><td colspan="2"><center>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp

</center></td></tr>
</table>
</body>
</html>

Is This A Good Question/Topic? 0

Replies To: Problem when converting units

#2 Dormilich

• 痛覚残留

Reputation: 3700
• Posts: 11,171
• Joined: 08-June 10

Re: Problem when converting units

Posted 12 January 2013 - 10:27 AM

the problem is .value2. since it is an invalid HTML attribute, the JS parser ignores it when building the document object.

therefore lines #6 & #8 should throw a TypeError (undefined has no properties) in the Error Console and stop the script execution.

This post has been edited by Dormilich: 12 January 2013 - 10:28 AM