title: if else nesting, and using a var toggle to id and change elements
body: <p>I have code that grabs an input field element 'distance', and converts it to miles.</p>
  
  <p>My problem is that it's applying the calc everytime the measurement dropdown is changed. This means the calc is applied for example, to kilometers, when kilometers is already selected. Below I have tried to implement a toggle to overcome this. But there has to be a better way.</p>
  
  <pre><code>&lt;script&gt;
      $('select').on('change', function (event) {
  
      var measurement = $('#measurement').val();
      var distance = $('#distance').val();
      var value = 0;
      var toggle = off;
      if (measurement == "miles")
      {
      value = (distance * 0.62137119);
      toggle = on;
      }
      else if (measurement == "kilometres")
      {
          if (toggle == on)
          {
          value = (distance / 0.62137119);
          toggle == off;
          }
      }
      $("#distance").val(value.toFixed(0))
      }); 
  &lt;/script&gt;
  </code></pre>
  
  <p>Q: How can I have a dropdown to select Miles/Kilometers, so that my element 'distance' shows correctly?</p>
  
  <p>Note: the element distance is a text input element element populated from a PHP MySQL DB query prior.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;select name="measurement" name="measurement" id="measurement"&gt;
  &lt;optgroup label="measurement"&gt;
  &lt;option value=""&gt;Please Select Imperial or Metric&lt;/option&gt;
  &lt;option value="kilometres" selected="selected"&gt;kilometres&lt;/option&gt;
  &lt;option value="miles"&gt;miles&lt;/option&gt;
  &lt;/optgroup&gt;
  &lt;/select&gt;
  &lt;br/&gt;
  &lt;input type="text" size="5" id="distance" name="distance" value="10" class="value" /&gt;
  </code></pre>
  
  <p>CodeIgniter Module to populate drop down list</p>
  
  <pre><code>&lt;?php
  class Measurement extends CI_Model
  {
      function get_measurement()
      {
          $query = $this-&gt;db-&gt;query('select * from measurement ORDER by name');
          if ($query-&gt;num_rows() &gt; 0) {
              foreach ($query-&gt;result_array() as $row) {
                  $measurementdata[$row['name']] = $row['name'];
              }
              return $measurementdata;
          } else {
              return false; // return false if no items for dropdown
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p><a href="http://jsfiddle.net/SJYw7/14/" rel="nofollow">http://jsfiddle.net/SJYw7/14/</a></p>