title: Determine if dynamic selects are defined with javascript
body: <p>I've stumbled across a problem and could use your help figuring out some logic here to deal with it. Basically I have a dynamic order form where the price is updated real time on the user side using javascript based on what options the user selects. The issue is that there are 3 dynamic dropdowns that might not all exist, ie sometimes there will be drop down a - c, sometimes there will only be a, or just a and b, etc. The error I'm getting is that the variable for any undefined select is undefined (obviously). I've implemented some js to deal with that but with no success. Here is the code I'm working with:</p>
  
  <p>PHP: </p>
  
  <pre><code>     $a_sql = ("SELECT * FROM prod_add WHERE pid = '$id' AND `group` = 'a'");
  $a_query = mysqli_query($dbconx, $a_sql);
  $a_Count = mysqli_num_rows($a_query);
  if ($a_Count &gt; 0) {
  
  
  
      while($row = mysqli_fetch_array($a_query)) {
  
      $a_option = $row["option"];
      $a_value = $row["value"];
  
      $a_results .= "&lt;option value='$a_value'&gt;$a_option&lt;/option&gt;";
  
      }
  
  }
  
  if($a_Count &gt; 0) {
  
      $a_opt = "&lt;select class='opt_drop' id='optA' name='optA' onchange='getTotal()'&gt;$a_results&lt;/select&gt;"; 
  
  } else { 
  
      $a_opt = "";
  
  }
  </code></pre>
  
  <p>Rinse and repeat for b &amp; c</p>
  
  <p>HTML: </p>
  
  <pre><code>   &lt;h2 id="totalPrice"&gt;$&lt;?php echo $base_cost ?&gt;&lt;/h2&gt; &lt;br /&gt;
            &lt;input type="hidden"  name="base_cost" id="base_cost" value="&lt;?php echo $base_cost ?&gt;" /&gt; 
            &lt;?php echo $a_opt; ?&gt;&lt;br /&gt;
            &lt;?php echo $b_opt; ?&gt;&lt;br /&gt;
            &lt;?php echo $c_opt; ?&gt;
              Quantity:
            &lt;select name="quantity" id="quantity" onchange="getTotal()"&gt;
                  &lt;option value="1"&gt; 1 &lt;/option&gt;
                  &lt;option value="2"&gt; 2 &lt;/option&gt;
                  &lt;option value="3"&gt; 3 &lt;/option&gt;
                  &lt;option value="4"&gt; 4&lt;/option&gt;
                  &lt;option value="5"&gt; 5 &lt;/option&gt;
                  &lt;option value="6"&gt; 6 &lt;/option&gt;
                  &lt;option value="7"&gt; 7 &lt;/option&gt;
                  &lt;option value="8"&gt; 8 &lt;/option&gt;
                  &lt;option value="9"&gt; 9 &lt;/option&gt;
                  &lt;option value="10"&gt; 10 &lt;/option&gt;
            &lt;/select&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>      &lt;script type="text/javascript"&gt;
  
       function getDDValue(elementID){
           var value = 0;
           var element = document.getElementById(elementID);
            value = element.options[element.selectedIndex].value;
           console.log(value);
           return parseFloat(value);
       }
       function getBase (elementID) {
          var value = 0;
          var element = document.getElementById(elementID);
          value = element.value;
          return parseFloat(value);
       }
  
       function getTotal(){
           if (typeof optA != "undefined") {
           var optA =  getDDValue("optA");
          } else {
              var optA = "0";
          }
          if (typeof optB != "undefined") {
              var optB =  getDDValue("optB");
          } else {
              var optB = "0";
          }
          if (typeof optC != "undefined") {
                var optC =  getDDValue("optC");
            } else {
               var optC = "0";
            }
            var quantity = getDDValue("quantity");
          var base_cost = getBase ("base_cost")
            var totalPrice = '0';
  
           totalPrice = parseFloat((+base_cost  + +optA + +optB + +optC) * +quantity);
           console.log(totalPrice);
            document.getElementById('totalPrice').innerHTML = "$" + totalPrice; 
       }
  
       getTotal();
       &lt;/script&gt;
  </code></pre>
  
  <p>As you can see above I tried determining if the value was undefined and if it was to just give it a value of 0. That didn't work though I can't be sure why. </p>