3

I'm having some difficulty creating a variable with another variable in jQuery. I don't know how to write the var side of the equation. Here's what I'm trying to create:

var $counter= 0;
    $('.selector').each(function(){
       $counter += 1;
       var newVariable-($counter) // this is where I'd like to create the new  
                                  // variable with the number from $counter at the end.
    });

with the goal to creating:

newVariable-1
newVariable-2
newVariable-3... 

and so on...

2
  • 1
    stackoverflow.com/questions/2895930/… Commented Jun 13, 2014 at 4:38
  • Readability tip: Try to use variables beginning with $ only for caching jQuery selections. (var $counter = $('#counter'); var counter = 0;) Commented Jun 13, 2014 at 4:41

3 Answers 3

8

You could create an object to hold these values but not dynamic variables.

var $counter= 0;
var variableHolder = {};
$('.selector').each(function(){
   $counter += 1;
   variableHolder["newVariable-"+$counter] = ...
});

Or if you want to make global variables (which is not recommended), you could use window:

var $counter= 0;
$('.selector').each(function(){
   $counter += 1;
   window["newVariable-"+$counter] = ...
});
Sign up to request clarification or add additional context in comments.

Comments

1

As others have pointed out, using an {} with square bracket notation will simplify this task greatly.

Something like this:

var myobj = {},
    prefix = 'my_cool_var';

for(var i = 0, len = 10; i < len; i++) {
    myobj[prefix + i] = undefined; // { my_cool_var + i : undefined }
}

// Setters - dot notation and square bracket
myobj.my_cool_var1 = "Hello!";
myobj['my_cool_var2'] = "Hello 2!";

// Getters - dot notation and square bracket
alert(myobj.my_cool_var1); // alerts Hello!
alert(myobj['my_cool_var2']); // alerts Hello 2!

Now, if you needed to expose the variables in a global scope (yuck - but hey, sometimes you gotta) so you don't need to specify an object (myobj), you can use window with square bracket notation in your for loop.

var prefix = 'my_global_var';

for(var i = 0, len = 10; i < len; i++) {
    window[prefix + i] = undefined; // creates global, my_global_var + i = undefined
}

my_cool_var1 = "Hello!";
alert(my_cool_var1); // alerts Hello!

Finally, if you search the web deep enough, you'll find eval examples like this:

var prefix = 'my_evil_var';

for(var i = 0, len = 10; i < len; i++) {
    // Don't do this. Use square bracket notation with window, if you need a global.
    eval(prefix + i + '= undefined') // creates global, my_evil_var + i = undefined
}

my_evil_var = "Eval abuse is bad!!";
alert(my_evil_var1); // alerts Eval abuse is bad!!

Hope this helps!

Comments

0

Just make use of the json in this context,

var $counter= 0;
var $newVar = {};

$('.selector').each(function(){
   $counter += 1;
   $newVar['newVariable-'+ ($counter)] = null;
});

so you can access it like $newVar.newVariable-1,.. $newVar.newVariable-N And please note that this is the best practice, we could do as you asked by accessing the window object, but that is not recommended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.