Variable variables in JavaScript within a loop

Variable variables in JavaScript within a loop


var x = 1;
var ctr = 5;
while (x<=ctr)
var ss_+x = $('input[name=IDMenu_'+x+']').val();
x++;



As you can see above I have a loop that will iterate through input fields with the name IDMenu_1 through to IDMenu_5. I attempted to make the variable names themselves variable by using the syntax var ss_+x; however this is incorrect and does not work.


IDMenu_1


IDMenu_5


var ss_+x



If I were to not use a loop, this is what I would be doing:


var ss_1 = $('input[name=IDMenu_1]').val();
var ss_2 = $('input[name=IDMenu_2]').val();
var ss_3 = $('input[name=IDMenu_3]').val();
var ss_4 = $('input[name=IDMenu_4]').val();
var ss_5 = $('input[name=IDMenu_5]').val();



Is it possible to accomplish this within a loop?






var ss_+x - what should that do? Have you had a look at your browser's error console while running the above code?

– Nico Haase
Sep 10 '18 at 8:55


var ss_+x






simply make the var an array and push the values to it. var ss = ; - then ss.push($('input....val());

– Jeff
Sep 10 '18 at 8:55



var ss = ;


ss.push($('input....val());






Possible duplicate of Variable variables in JavaScript

– Scoots
Sep 10 '18 at 9:48




2 Answers
2



You're trying to create variables with dynamic names so you could use window[variable_name].


window[variable_name]



NOTE: IMO you could use an array of values instead in this case.




var x = 1;
var ctr = 5;

while (x <= ctr)
window["ss_" + x] = $('input[name=IDMenu_' + x + ']').val();
x++;


console.log(ss_1, ss_2, ss_3, ss_4, ss_5);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input name="IDMenu_1" value="10">
<input name="IDMenu_2" value="20">
<input name="IDMenu_3" value="30">
<input name="IDMenu_4" value="40">
<input name="IDMenu_5" value="50">






This is working and I prefer this one than using array, I don't know which is better but for me this is more simple than using array. Thanks.

– ICG DEVS
Sep 10 '18 at 9:08






Welcome, Glad I could help.

– Zakaria Acharki
Sep 10 '18 at 9:14






@ICGDEVS Also, you could consider not putting all your variables in the global scope, only create one ss object that will contain your properties, by doing the following : var ss = ; while(...) ss[x] = ...; and then access them with ss.1, ss.2, ...

– Logar
Sep 10 '18 at 9:47



ss


var ss = ; while(...) ss[x] = ...;


ss.1, ss.2, ...



It'd be better to make the var an array, then push the items to it.


var x = 1;
var ctr = 5;
var ss = ;
while (x<=ctr)
ss.push($('input[name=IDMenu_'+x+']').val());
x++;



You'll then have the values in


console.log(ss[0]);
console.log(ss[1]);
// ...



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)