title: Call javascript/jQuery function onchange product option on Magento product page
body: <p>My task is to make a box on magento product page with total price for quantity selected. In other words qty * price. So I did this:</p>
  
  <pre><code>$j = jQuery.noConflict();
  
  function get_total_qty() {
      var qt_Price = parseInt(0);
      $j('.price').each(function() {
          if (this.offsetParent.className != 'dropdown-cart no_quickshop') {
              qt_Price = parseFloat(this.textContent);
          }
      });
  
  
      /*
       * AJAX call
       */
      var quantity = $j('#qty').val(); // get final quantity
      var price = qt_Price; // get price
      $j.post("http://example.com/myscripts/ajaxPriceCal.php", {
              qty: quantity,
              pr: price
          },
          function(data) {
              var qt_total_price = data;
              $j('#totpr').html(qt_total_price);
          });
  
  }
  
  $j(document).ready(function() {
      get_total_qty();
  });
  </code></pre>
  
  <p>And I call get_total_qty() function every time button to increase or decrease qty is pressed. Now I tried the same with drop-down lists of product options but it didn't work as I expected. It calls the function before reloading product price which I use for calculation.</p>
  
  <p>E.g. product is $10, qty is 2, total $20 if option selected (+$5) then what I get displayed is price $15, qty 2, total $20. Because function was called before updating .price class element. If I change qty it displays correct total again. </p>
  
  <p>Perhaps my way of doing these calculations is silly. I'm open to suggestions on how to do it right way. Yet my pressing issue is calling the function on right time. What files should I edit and where should I place the call?</p>
  
  <p>Sorry if my question appears silly i'm new in JS or jquery...</p>
  
  <p>EDIT -></p>
  
  <p>/app/design/frontend/base/default/template/catalog/product/view/type/options/configurable.phtml file generates options selection drop-downs here is the code:</p>
  
  <pre><code>&lt;?php
  $_product    = $this-&gt;getProduct();
  $_attributes = Mage::helper('core')-&gt;decorateArray($this-&gt;getAllowAttributes());
  ?&gt;
  &lt;?php if ($_product-&gt;isSaleable() &amp;&amp; count($_attributes)):?&gt;
      &lt;dl&gt;
      &lt;?php foreach($_attributes as $_attribute): ?&gt;
          &lt;dt&gt;&lt;label class="required"&gt;&lt;em&gt;*&lt;/em&gt;&lt;?php echo $_attribute-&gt;getLabel() ?&gt;&lt;/label&gt;&lt;/dt&gt;
            &lt;dd&lt;?php if ($_attribute-&gt;decoratedIsLast){?&gt; class="last"&lt;?php }?&gt;&gt;
              &lt;div class="input-box"&gt;
                  &lt;select onchange="get_total_qty()" name="super_attribute[&lt;?php echo $_attribute-&gt;getAttributeId() ?&gt;]" id="attribute&lt;?php echo $_attribute-&gt;getAttributeId() ?&gt;" class="required-entry super-attribute-select"&gt;
                    &lt;option&gt;&lt;?php echo $this-&gt;__('Choose an Option...') ?&gt;&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/div&gt;
            &lt;/dd&gt;
      &lt;?php endforeach; ?&gt;
      &lt;/dl&gt;
  &lt;script type="text/javascript"&gt;
      var spConfig = new Product.Config(&lt;?php echo $this-&gt;getJsonConfig() ?&gt;);
  &lt;/script&gt;
  &lt;?php endif;?&gt;
  </code></pre>
  
  <p>the result of it is this HTML on product page:</p>
  
  <pre><code>&lt;dl&gt;
          &lt;dt&gt;&lt;label class="required"&gt;&lt;em&gt;*&lt;/em&gt;Color&lt;/label&gt;&lt;/dt&gt;
      &lt;dd&gt;
          &lt;div class="input-box"&gt;
              &lt;select onchange="get_total_qty()" name="super_attribute[272]" id="attribute272" class="required-entry super-attribute-select"&gt;
                  &lt;option&gt;Pasirinkite...&lt;/option&gt;
                &lt;/select&gt;
            &lt;/div&gt;
      &lt;/dd&gt;
          &lt;dt&gt;&lt;label class="required"&gt;&lt;em&gt;*&lt;/em&gt;Puokštės dydis&lt;/label&gt;&lt;/dt&gt;
      &lt;dd class="last"&gt;
          &lt;div class="input-box"&gt;
              &lt;select onchange="get_total_qty()" name="super_attribute[975]" id="attribute975" class="required-entry super-attribute-select"&gt;
                  &lt;option&gt;Pasirinkite...&lt;/option&gt;
                &lt;/select&gt;
            &lt;/div&gt;
      &lt;/dd&gt;
  &lt;/dl&gt;
  &lt;script type="text/javascript"&gt;
      var spConfig = new Product.Config({"attributes":{"272":{"id":"272","code":"color","label":"Color","options":[{"id":"26","label":"Red","price":"0","oldPrice":"0","products":["177","178"]}]},"975":{"id":"975","code":"puokstes_dydis","label":"Puok\u0161t\u0117s dydis","options":[{"id":"136","label":"Didel\u0117","price":"30","oldPrice":"30","products":["178"]},{"id":"137","label":"Vidutin\u0117","price":"20","oldPrice":"20","products":["177"]}]}},"template":"#{price}\u00a0Lt","basePrice":"10","oldPrice":"10","productId":"175","chooseText":"Pasirinkite...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":21,"currentTax":0,"inclTaxTitle":"Incl. Mokestis"}});
  &lt;/script&gt;
  </code></pre>
  
  <p>As you can see I added <code>onchange="get_total_qty()"</code> in <code>&lt;select&gt;</code> element</p>
  
  <p>The JS code for <code>get_total_qty()</code> sits in .js file which is linked in <code>&lt;head&gt;</code>. Here is the function code:</p>
  
  <pre><code>function get_total_qty(){
  var price = parseInt(0);
  $j('.price').each(function(){
      if(this.offsetParent != null &amp;&amp; this.offsetParent.className != 'dropdown-cart no_quickshop' &amp;&amp; this.offsetParent.className != 'product'){ //makes sure it takes content from the right .price class element
          price = parseFloat(this.textContent);   
      }
  });
  
  var quantity = $j('#qty').val(); // get final quantity
  /*
  * AJAX call
  */
  $j.post("http://example.com/myscripts/ajaxPriceCal.php", { qty: quantity, pr: price },
                  function(data){
                      var qt_total_price = data;
                      $j('#totpr').html(qt_total_price );
  });     
  }
  </code></pre>
  
  <p>The same function is called everytime quantity is decreased or increased</p>
  
  <pre><code>&lt;input type="text" name="qty" id="qty" maxlength="12" value="1" title="Vnt." class="input-text qty" /&gt;
      &lt;div class="qty-ctl"&gt;
          &lt;button onclick="changeQty(1); return false;" class="increase"&gt;increase&lt;/button&gt;
          &lt;button onclick="changeQty(0); return false;" class="decrease"&gt;decrease&lt;/button&gt;
      &lt;/div&gt;
      &lt;div style="float:left;"&gt;&lt;span id="totpr"&gt;&lt;/span&gt;&lt;/div&gt;
  
              &lt;button type="button" title="PIRKTI" class="button btn-cart" onclick="productAddToCartForm.submit(this)"&gt;&lt;span&gt;&lt;span&gt;PIRKTI&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;
              &lt;script type="text/javascript"&gt;
      function changeQty(increase) {
          var qty = parseInt($('qty').value);
          if ( !isNaN(qty) ) {
              qty = increase ? qty+1 : (qty&gt;1 ? qty-1 : 1);
              $('qty').value = qty;
          }; get_total_qty();
      }
      &lt;/script&gt;
  </code></pre>
  
  <p>Next to the buttons is Div which holds total price. It updates as I want it to after qty changed but it does not reload after option selection made... well it does kind of but the function grabs price of previous selection not the new one that user just selected... which means that <code>&lt;span id="totpr"&gt;</code> text gets reloaded before item price (base price + selection price)</p>
  
  <p>I'm sorry english is not my mother tongue I hope I explained everything clearly :)</p>