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.
I have a dynamically changing <input>
elements, where their ids and values are created by PHP. So, total number <input>
elements there can be 1 or even 10+.
HTML:
<input id='photonumb1' type='text' value='001'/>
<input id='photonumb2' type='text' value='002'/>
<input id='photonumb3' type='text' value='003'/>
...
There is also one <input id="totalphotos" />
tag which has a value of how many <input>
elements there are created.
I want JavaScript to take the number of <input>
elements there are (probably taking a value from "totalnumbers
") and take the values of all "photonumb*
" (using for()
loop I suppose?). Each value of each element is to be assigned as new variable, like:
var photoname1 = 001;
var photoname2 = 002;
var photoname3 = 003;
...
Total variable number depends on how many <input>
elements there are created.
I was trying to do the suggestions, like:
totalphotos = document.getElementById('totalphotos').value;
for (i = 1; i <= totalphotos.length; i++) {
window[photoname + i] = document.getElementById('photonumb' + i).value;
}
But this did not help me, it just does not create a new variable, if I do console.log(photoname1);
for example.
Anyone has a suggesstion for me? Thank you in advance!
----- 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.