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;
}
-->
</style></head> 

<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>
Member Avatar for LastMitch

@christine_87

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

There's something wrong with your <input>'s:

<input type="text" id="amt" onclick="if(this.value==history1[history1.length-1]) this.value='';"> 

<input type="button" value="Convert >>" onclick="document.getElementById('amt').focus();convertVals()"> 
<input type="text" id="amt2"> 

Try this:

<input type="text" id="amt" valve="0" onfocus="this.select()" /> 

<input type="button" onClick="convertVals();" value=">>" />

<input type="text" id="amt2" valve="0" onfocus="this.select()" /> 

Thanks, still does not function

Member Avatar for LastMitch

@christine_87

Thanks, still does not function

Where did you get this code? The reason why I ask because you did you get any errors when you ran this code? Does this code involve with other files?

Instead of this which is line 59 to line 61:

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

just put it inside of function setUpBox():

function setUpBox() { 

var 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")

var 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)

var types = new Array("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)
} 

Regarding about your <input> tags:

<input type="text" id="amt" onclick="if(this.value==history1[history1.length-1]) this.value='';">
<input type="button" value="Convert >>" onclick="document.getElementById('amt').focus();convertVals()">
<input type="text" id="amt2"> 

From this

<input type="text" id="amt" onclick="if(this.value==history1[history1.length-1]) this.value='';">
<input type="button" value="Convert >>" onclick="document.getElementById('amt').focus();convertVals()">
<input type="text" id="amt2"> 

Try this:

<input type="text" id="amt" valves="" onclick="this.value=''">
<form name="sel1" method="post">
<script type="text/javascript"> setUpBox() </script>        
</form>
<form name="sel2" method="post">
<script type="text/javascript"> setUpBox() </script>
</form>
<input type="button" value="Convert >>" onclick="convertVals()"> 
<input type="text" id="amt2" valves=""> 

Thanks LastMitch, A friend gave me a php file with that code, because we were in a project, but he left me alone in the project and now I try to fix all this.

I did the change in the code and I have this error

SCRIPT5009: 'weights' undefined
convertion.php, Línea 41 Carácter 19

To solve this error, I did this

var weights = new Array
  function setUpBox(){...

but still the result is not observed into the textbox

Member Avatar for LastMitch

@christine_87

but still the result is not observed into the textbox

This line:

var resultStr = '<select name="conv" size=5 onchange="convertVals()">'

It should be var resultStr = '<select name="conv">'

because you already have convertVals() in the <input> tags already.

I never seen this kind of <input> tags:

<input type="text" id="amt" onclick="if(this.value==history1[history1.length-1]) this.value='';">

It should look like this:

<input type="text" id="amt" onclick="document.getElementById('amt')">

That's the reason why I ask from my previous post where did you get this code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.