title: jQuery - storing variables from PHP foreach loop
body: <p>I'm just having a play around, trying to sharpen my jQuery knowledge and I've run into an issue I can't wrap my head around. I'm using <a href="/questions/tagged/laravel" class="post-tag" title="show questions tagged &#39;laravel&#39;" rel="tag">laravel</a>, just as a side note, but this would also apply to standard PHP.</p>
  
  <p>I'm essentially retrieving a collection of data that I want to manipulate with jQuery. The list that is retrieved will constantly vary in size; for example:</p>
  
  <ul>
  <li>There are a collection of objects.</li>
  <li>Each object has a certain amount of items attached to it.</li>
  <li>Each item has a name and amount.</li>
  <li>When retrieved I want to be able to display the name and amount of each item.</li>
  <li>If I then change the amount. I want each items amount to change accordingly.</li>
  </ul>
  
  <p><strong>Here is my Code:</strong></p>
  
  <pre><code>@foreach($items as $item)
  &lt;li&gt;&lt;span class="item_amount"&gt;{{ $item-&gt;amount }}&lt;/span&gt;{{ $item-&gt;name }}&lt;/li&gt;
  @endforeach 
  </code></pre>
  
  <p>This retrieves all the items associated to this object:
  In this case we see something like:</p>
  
  <ul>
  <li>400 item1</li>
  <li>67 item2</li>
  <li>830 item3</li>
  </ul>
  
  <p>I've a very simple HTML <code>&lt;select&gt;</code> input:</p>
  
  <pre><code>&lt;select id="steven" class="formDropdown"&gt;
      &lt;option&gt;Select item amount&lt;/option&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;/select&gt;
  </code></pre>
  
  <p>What I then want to do is retrieve the amount of each item, store it as a variable in jQuery and then when the <code>&lt;select&gt;</code> amount is changed, the item amount will by multiplied by it. </p>
  
  <p>For example if 2 was selected from the select input the list would change to:</p>
  
  <ul>
  <li>800 item1</li>
  <li>134 item2</li>
  <li>1660 item3</li>
  </ul>
  
  <p>I've figured out that the best way to retrieve the amount is to set the variable as the PHP.</p>
  
  <pre><code>var originalAmount = '{{ $item-&gt;amount }}';
  </code></pre>
  
  <p>My reason for doing it this way is because if I select the value of the HTML within the <code>&lt;span&gt;</code> tag, each time I multiply it multiplies the displayed amount rather than the original amount. An example:</p>
  
  <blockquote>
    <p>If the amount was 10 and I multiply by 3 the result is 30, if iI then multiply by 2, expecting 20 (10*2) I obviously now get 60.</p>
  </blockquote>
  
  <p>Not what I want at all.</p>
  
  <p>As you can see my problem here is that I need to set a variable for each of the items in my foreach loop, where I've tried doing something like this:</p>
  
  <pre><code>$( ".item_amount" ).each(function( index ) {
      alert( amount+ ": " + $( this ).text() );
  });
  </code></pre>
  
  <p>Which alerts me the amount of each in order, all well and good however I now want to store those amounts as a variable that correspond to it's position in the list:</p>
  
  <p>I've tried to do something like this to get a dynamic variable name:</p>
  
  <pre><code>@foreach($items as $item)
  &lt;li&gt;&lt;span class="item_amount{{$item-&gt;amount}}"&gt;{{ $item-&gt;amount }}&lt;/span&gt;{{ $item-&gt;name }}&lt;/li&gt;
  @endforeach
  </code></pre>
  
  <p>This will give me a dynamic class which I could then select and manipulate which is perfect.</p>
  
  <p>However how do I store each of these as a variable inside jQuery?</p>
  
  <ol>
  <li>variable 1 would be named "item_amount400" with value of 400</li>
  <li>variable 2 would be named "item_amount67" with value of 67</li>
  <li>variable 3 would be named "item_amount830" with value of 830</li>
  </ol>
  
  <p>Which I want to achieve doing what I've mentioned above:</p>
  
  <pre><code>var item_amount400 = '{{ $item-&gt;amount }}';
  </code></pre>
  
  <p>I'm probably doing this the most back to front way and thinking way too much into this. </p>
  
  <p>Could the variable be created inside the foreach loop and then retrieve all of them outside the loop? E.g. like this:</p>
  
  <pre><code>@foreach($items as $item)
  &lt;li&gt;&lt;span class="item_amount{{$item-&gt;amount}}"&gt;{{ $item-&gt;amount }}&lt;/span&gt;{{ $item-&gt;name }}&lt;/li&gt;
  &lt;script&gt;
      make variable here
  &lt;/script&gt;
  @endforeach
  </code></pre>
  
  <p>If so, how would I go about doing that and how can I make the variables global and know exactly what to retrieve?</p>
  
  <p>Like I mentioned each objects have a different amount of items, some may have 3 some may have 30.</p>