title: Run jQuery Coffeescript on input change
body: <p>I have a jQuery Coffeescript function that runs on page load. The functions grabs a table row then it's price, quantity and discounts and processes the values to determine a total for the row. </p>
  
  <p>I currently run the function on page load but would also like it to run anytime that the form input values change. Please Note, this form is used on a rails f.fields_for which generates multiple forms and also additional dynamically generated forms. </p>
  
  <p>What would be the most DRY way to do this? </p>
  
  <pre><code>$('tr.order-item').each -&gt;
    q = $(this).find('.quantity_input')
      .val()
    quantity = parseInt(q)
    p = $(this).find('.price').text().replace("$", "")
    price = parseFloat(p.replace(/,/g, ''), 10)
    d = $(this).find('.discount_input').val()
    discount = parseFloat(d)
    total = price * quantity - discount
    total_price = "$"+ parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString()
    $(this).find('.item-total').text(total_price)
  </code></pre>
  
  <p>form_for fields</p>
  
  <pre><code>&lt;tr class="order-item"&gt;
  &lt;td&gt;
      &lt;div class="btn-group"&gt;
          &lt;button type="button" class="btn-u dropdown-toggle" data-toggle="dropdown"&gt;
              &lt;i class="fa fa-angle-down"&gt;&lt;/i&gt;
          &lt;/button&gt;
          &lt;ul class="dropdown-menu" role="menu"&gt;
              &lt;li&gt;
              &lt;/li&gt;
              &lt;%= f.hidden_field :_destroy %&gt;
              &lt;%= link_to "Remove Item", '#', class: "remove_fields btn btn-link" %&gt;
              &lt;li&gt;&lt;button class="btn btn-link"&gt;Add Custom Item&lt;/button&gt;&lt;/li&gt;
              &lt;li&gt;&lt;button class="btn btn-link"&gt;Remove Item&lt;/button&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;h5 class="product"&gt;&lt;%= f.object.product.name %&gt;&lt;/h5&gt;
      &lt;%= f.hidden_field :product_id, class:"product-id" %&gt;
      &lt;%= f.hidden_field :id, class:"id" %&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;h5 class="text-right"&gt;&lt;span style="color: green" class="price"&gt;&lt;%= number_to_currency f.object.unit_price %&gt;&lt;/span&gt;&lt;/h5&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;%= f.number_field :quantity, min: 1, hide_label: true, class:"quantity_input" %&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;%= f.text_field :discount, hide_label: true, class:"discount_input" %&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;h5 class="text-right"&gt;&lt;span style="color: green" class="item-total"&gt;&lt;/span&gt;&lt;/h5&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p></p>