Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright first off this is in no way the best way to do this. I took a lot of liberties with the answer and hard coded the values.</p> <p><strong><a href="http://jsfiddle.net/loktar/EfYev/7/" rel="nofollow">Live Demo</a></strong></p> <p>I cleaned up the html a little bit, but mainly only made changes to the JavaScript.</p> <p>First thing I did was like I said hard code the values.</p> <pre><code>var fruits = { apple: { apple: 1, banana: 0.5, pineapple: 8 }, banana: { apple: 2, banana: 1, pineapple: 16 }, pineapple: { apple: 0.125, banana: 0.0625, pineapple: 1 } }; </code></pre> <p>Now personally I would make your HTML easier to read and iterate via JS to create the object dynamically. That is of course if you are expecting different values from the server and the <em>only</em> way to get them for some reason is via the HTML data attributes.</p> <p>The next thing I did was change your click event to the following.</p> <pre><code> var vals = { apple: 0, banana: 0, pineapple: 0 }; $('.dropdown').each(function () { var fruitVal = $(this).prev('.number').val(); if (fruits[$(this).val()]) { vals.apple += fruitVal * fruits[$(this).val()].apple; vals.banana += fruitVal * fruits[$(this).val()].banana; vals.pineapple += fruitVal * fruits[$(this).val()].pineapple; } }); $('.apple').text(vals.apple); $('.banana').text(vals.banana); $('.pineapple').text(vals.pineapple); </code></pre> <p>Again I would have dynamically made the <code>vals</code> object based on the data received earlier. I would have iterated the top properties from the fruits object to create it.</p> <p>In my opinion its weird to have two calculate buttons. Also I <em>hate</em> using prev to get a value, but you had no class assigned to the inputs so basically no way to reference what input belonged to what drop down.</p> <p>Anyway once you get the values it just comes down to basic math. If you have any questions feel free to ask. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload