title: Jquery working on first select box, but copied code doesn't work on second
body: <p>I am using the following to pull records from my mysql database using ajax. It then works out the line totals for each by multiplying the <code>qty</code> box by the <code>priceeach</code>. The problem I'm having is the first select works, but the second one doesn't, and in the console I am getting the following error message:</p>
  
  <pre><code>  Uncaught ReferenceError: change is not defined on line 97
  </code></pre>
  
  <p>which is this line :</p>
  
  <pre><code>                    options += data[x]['priceeach']; 
                  } // &lt;-- This Line
                  $('#priceeach1').text(options);
              });
          });
  </code></pre>
  
  <p>of this script:</p>
  
  <pre><code>&lt;script type="text/javascript" charset="utf-8"&gt;
      $(document).ready(function(){
          $('#stock1').on('change', function (){
              $('#qty1').val('');
              $('#linetotal1').text('');
              var newValue1 = $.getJSON('select2.php', {stockID: $(this).val()}, function(data){
                  var options = '';
                  for (var x = 0; x &lt; data.length; x++) { 
                      options += data[x]['priceeach']; 
                  }
                  $('#priceeach1').text(options);
              });
          });
          $('#qty1').on('keyup', function (){
              var qty1 = $("#qty1").val();
              var priceeach1 =  $("#priceeach1").html();
              var total=parseFloat(priceeach1)  * parseInt(qty1);
              $('#linetotal1').html(total);
          });
  
          $('#stock2').on('change', function (){
              $('#qty2').val('');
              $('#linetotal2').text('');
              var newValue2 = $.getJSON('select2.php', {stockID: $(this).val()}, function(data){
                  var options = '';
                  for (var x = 0; x &lt; data.length; x++) { 
                      options += data[x]['priceeach']; 
                  }
                  $('#priceeach2').text(options);
              });
          });
          $('#qty2').on('keyup', function (){
              var qty2 = $("#qty2").val();
              var priceeach2 =  $("#priceeach2").html();
              var total2=parseFloat(priceeach2)  * parseInt(qty2);
              $('#linetotal2').html(total2);
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Following is the PHP/HTML:</p>
  
  <pre><code>&lt;select id="customer"&gt;
      &lt;option&gt;Please Select / Search For A Customer&lt;/option&gt;
      &lt;?php
          $sql = &lt;&lt;&lt;SQL
              SELECT *
              FROM `customers`
              SQL;
          if(!$result = $db-&gt;query($sql)){ die('There was an error running the query [' . $db-&gt;error . ']');}
          while($row = $result-&gt;fetch_assoc()){
              if ($row['bussinessname'] == ''){$name = $row['title'].' '.$name = $row['firstname'].' '.$name = $row['surname'];}
              else {$name = $row['bussinessname'];}
              echo '&lt;option value="'.$row['customerID'].'"&gt;'.$name.'&lt;/option&gt;';
          }
          echo '&lt;/select&gt;';
      ?&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;select id="vehicle"&gt;
      &lt;option&gt;Select A Customers Vehicle&lt;/option&gt;
  &lt;/select&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;div class="stock1"&gt;
      &lt;select id="stock1"&gt;
          &lt;option&gt;Please Select / Search For Stock&lt;/option&gt;
          &lt;?php
              $sql2 = &lt;&lt;&lt;SQL
                  SELECT *
                  FROM `stock`
                  SQL;
              if(!$result2 = $db-&gt;query($sql2)){ die('There was an error running the query [' . $db-&gt;error . ']');}
              while($row2 = $result2-&gt;fetch_assoc()){
                  $instock = $row2['numberofstock'] - $row2['numberofstocksold'];
                  if ($row2['numberofstocksold'] &gt;= $row2['numberofstock']){$stockinfo = '';}
                  else {$stockinfo = $row2['make'].' '.$row2['model'].' - '.$row2['tyrewidth'].'/'.$row2['tyreprofile'].'/'.$row2['tyresize'].'/'.$row2['tyrespeed'].'/'.$row2['tyreload'].' - ('.$instock.' In Stock) - £'.$row2['priceeach'].' Each';}
                  echo '&lt;option value="'.$row2['stockID'].'"&gt;'.$stockinfo.'&lt;/option&gt;';
              }
          ?&gt;
      &lt;/select&gt;   X
      &lt;input style="width:40px;" type="text" name="qty1" id="qty1" /&gt;
      &lt;span style="display:none;"  id="priceeach1"&gt;&lt;/span&gt;
      - Line Total : &amp;pound;&lt;span id="linetotal1"&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class="stock2"&gt;
      &lt;select id="stock2"&gt;
          &lt;option&gt;Please Select / Search For Stock&lt;/option&gt;
          &lt;?php
              $sql3 = &lt;&lt;&lt;SQL
                  SELECT *
                  FROM `stock`
                  SQL;
              if(!$result3 = $db-&gt;query($sql3)){ die('There was an error running the query [' . $db-&gt;error . ']');}
              while($row3 = $result3-&gt;fetch_assoc()){
                  $instock = $row3['numberofstock'] - $row3['numberofstocksold'];
                  if ($row3['numberofstocksold'] &gt;= $row3['numberofstock']){$stockinfo = '';}
                  else {$stockinfo = $row3['make'].' '.$row3['model'].' - '.$row3['tyrewidth'].'/'.$row3['tyreprofile'].'/'.$row3['tyresize'].'/'.$row3['tyrespeed'].'/'.$row3['tyreload'].' - ('.$instock.' In Stock)';}
                  echo '&lt;option value="'.$row3['stockID'].'"&gt;'.$stockinfo.'&lt;/option&gt;';
              }
          ?&gt;
      &lt;/select&gt;   X
      &lt;input style="width:40px;" type="text" name="qty2" id="qty2" /&gt;
      &lt;span style="display:none;"  id="priceeach2"&gt;&lt;/span&gt;  - 
      Line Total : &amp;pound;&lt;span id="linetotal2"&gt;&lt;/span&gt;   
  &lt;/div&gt;
  &lt;div class="stock3"&gt;
      &lt;select id="stock3"&gt;
          &lt;option&gt;Please Select / Search For Stock&lt;/option&gt;
          &lt;?php
              $sql4 = &lt;&lt;&lt;SQL
                  SELECT *
                  FROM `stock`
                  SQL;
              if(!$result4 = $db-&gt;query($sql4)){ die('There was an error running the query [' . $db-&gt;error . ']');}
              while($row4 = $result4-&gt;fetch_assoc()){
                  $instock = $row4['numberofstock'] - $row4['numberofstocksold'];
                  if ($row4['numberofstocksold'] &gt;= $row4['numberofstock']){$stockinfo = '';}
                  else {$stockinfo = $row4['make'].' '.$row4['model'].' - '.$row4['tyrewidth'].'/'.$row4['tyreprofile'].'/'.$row4['tyresize'].'/'.$row4['tyrespeed'].'/'.$row4['tyreload'].' - ('.$instock.' In Stock)';}
                  echo '&lt;option value="'.$row4['stockID'].'"&gt;'.$stockinfo.'&lt;/option&gt;';
              }
          ?&gt;
      &lt;/select&gt;   X 
      &lt;input style="width:40px;" type="text" name="qty3" id="qty3" /&gt;
      &lt;span style="display:none;"  id="priceeach3"&gt;&lt;/span&gt;  - 
      Line Total : &amp;pound;&lt;span id="linetotal3"&gt;&lt;/span&gt;       
  &lt;/div&gt;
  &lt;div class="stock4"&gt;
      &lt;select id="stock4"&gt;
          &lt;option&gt;Please Select / Search For Stock&lt;/option&gt;
          &lt;?php
              $sql5 = &lt;&lt;&lt;SQL
                  SELECT *
                  FROM `stock`
                  SQL;
              if(!$result5 = $db-&gt;query($sql5)){ die('There was an error running the query [' . $db-&gt;error . ']');}
              while($row5 = $result5-&gt;fetch_assoc()){
                  $instock = $row5['numberofstock'] - $row5['numberofstocksold'];
                  if ($row5['numberofstocksold'] &gt;= $row5['numberofstock']){$stockinfo = '';}
                  else {$stockinfo = $row5['make'].' '.$row5['model'].' - '.$row5['tyrewidth'].'/'.$row5['tyreprofile'].'/'.$row5['tyresize'].'/'.$row5['tyrespeed'].'/'.$row5['tyreload'].' - ('.$instock.' In Stock)';}
                  echo '&lt;option value="'.$row5['stockID'].'"&gt;'.$stockinfo.'&lt;/option&gt;';
              }
          ?&gt;
      &lt;/select&gt;   X 
      &lt;input style="width:40px;" type="text" name="qty4" id="qty4" /&gt;
      &lt;span style="display:none;"  id="priceeach4"&gt;&lt;/span&gt;  - 
      Line Total : &amp;pound;&lt;span id="linetotal4"&gt;&lt;/span&gt;       
  &lt;/div&gt;
  &lt;div class="stock5"&gt;
      &lt;select id="stock5"&gt;
          &lt;option&gt;Please Select / Search For Stock&lt;/option&gt;
          &lt;?php
              $sql6 = &lt;&lt;&lt;SQL
                  SELECT *
                  FROM `stock`
                  SQL;
              if(!$result6 = $db-&gt;query($sql6)){ die('There was an error running the query [' . $db-&gt;error . ']');}
              while($row6 = $result6-&gt;fetch_assoc()){
                  $instock = $row6['numberofstock'] - $row6['numberofstocksold'];
                  if ($row6['numberofstocksold'] &gt;= $row6['numberofstock']){$stockinfo = '';}
                  else {$stockinfo = $row6['make'].' '.$row6['model'].' - '.$row6['tyrewidth'].'/'.$row6['tyreprofile'].'/'.$row6['tyresize'].'/'.$row6['tyrespeed'].'/'.$row6['tyreload'].' - ('.$instock.' In Stock)';}
                  echo '&lt;option value="'.$row6['stockID'].'"&gt;'.$stockinfo.'&lt;/option&gt;';
              }
          ?&gt;
      &lt;/select&gt;   X 
      &lt;input style="width:40px;" type="text" name="qty5" id="qty5" /&gt;
      &lt;span style="display:none;"  id="priceeach5"&gt;&lt;/span&gt;  - 
      Line Total : &amp;pound;&lt;span id="linetotal5"&gt;&lt;/span&gt;       
  &lt;/div&gt;
  </code></pre>