function showChange() { var select = document.getElementById("x"); var value = parseInt(select.options[select.selectedIndex].text); alert(value); } function measureDistance() { var select = document.getElementById("x"); var valueX = parseInt(select.options[select.selectedIndex].text); select = document.getElementById("y"); var valueY = parseInt(select.options[select.selectedIndex].text); var result = Math.Sqrt(Math.Pow(valueY, 2) + Math.Pow(valueX, 2)) alert(result); } 
 <body> <FORM name="form"> <select id="x" name="x" onChange="showChange()"> <option name="0"> 0</option> <option name="1"> 1</option> <option name="2"> 2</option> </select> <select id="y" name="y" onChange="measureDistance()"> <option name="0"> 0</option> <option name="1"> 1</option> <option name="2"> 2</option> </select> </FORM> </body> 

I don’t understand why showChange() works, but measureDistance () doesn’t. The code should perform a simple task of calculating the coordinates (using select ) the distance to the point. Google Chrome does not work.

  • Math.sqrt , Math.pow - Igor
  • I use when calculating, but this, apparently, is not enough. - ti-phonex
  • Be careful. - Igor
  • Do you mean that these functions should be used separately? - ti-phonex
  • I made you a working example. Click on the "Run code" button. Select a value in the second select. Read the error message. - Igor

1 answer 1

Math.sqrt , Math.pow

 function showChange() { var select = document.getElementById("x"); var value = parseInt(select.options[select.selectedIndex].text); alert(value); } function measureDistance() { var select = document.getElementById("x"); var valueX = parseInt(select.options[select.selectedIndex].text); select = document.getElementById("y"); var valueY = parseInt(select.options[select.selectedIndex].text); var result = Math.sqrt(Math.pow(valueY, 2) + Math.pow(valueX, 2)) alert(result); } 
  <FORM name="form"> <select id="x" name="x" onChange="showChange()"> <option name="0"> 0</option> <option name="1"> 1</option> <option name="2"> 2</option> </select> <select id="y" name="y" onChange="measureDistance()"> <option name="0"> 0</option> <option name="1"> 1</option> <option name="2"> 2</option> </select> </FORM>