title: Dynamic javascript variable name using for loop
body: <p>I have a problem. Was working on this for a long time, but really stuck. Searching in stackoverflow for similar problems did not really help me - as people have a little different issues and I could not implement the answers to my code.</p>
  
  <p>I have a dynamically changing <code>&lt;input&gt;</code> elements, where their ids and values are created by PHP. So, total number <code>&lt;input&gt;</code> elements there can be 1 or even 10+.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;input id='photonumb1' type='text' value='001'/&gt; 
  &lt;input id='photonumb2' type='text' value='002'/&gt; 
  &lt;input id='photonumb3' type='text' value='003'/&gt; 
  ...
  </code></pre>
  
  <p>There is also one <code>&lt;input id="totalphotos" /&gt;</code> tag which has a value of how many <code>&lt;input&gt;</code> elements there are created.</p>
  
  <p>I want JavaScript to take the number of <code>&lt;input&gt;</code> elements there are (probably taking a value from "<code>totalnumbers</code>") and take the values of all "<code>photonumb*</code>" (using <code>for()</code> loop I suppose?). Each value of each element is to be assigned as new variable, like:</p>
  
  <pre><code>var photoname1 = 001;
  var photoname2 = 002;
  var photoname3 = 003;
  ...
  </code></pre>
  
  <p>Total variable number depends on how many <code>&lt;input&gt;</code> elements there are created.</p>
  
  <p>I was trying to do the suggestions, like: </p>
  
  <pre><code>totalphotos = document.getElementById('totalphotos').value;
  
  for (i = 1; i &lt;= totalphotos.length; i++) { 
      window[photoname + i] = document.getElementById('photonumb' + i).value;
  }
  </code></pre>
  
  <p>But this did not help me, it just does not create a new variable, if I do <code>console.log(photoname1);</code> for example. </p>
  
  <p>Anyone has a suggesstion for me? Thank you in advance!</p>
  
  <p>----- EDIT---
  Hey  @Quentin, why marked as duplicate? Maybe the idea of the question might be similar, but I am asking it completely other way, so even the answer that I am looking for is different. Moreover, I have seen that question before, I could not completely understand how it works. Thus for other beginners like me, this question might be way more useful. Thanks.</p>