How can i join a string and a variable to create a new variable in javascript?

Jorgen

Found a couple of solutions for php, but couldn't find any for javascript.

Basically I am trying to make a bunch of eventlisteners for 30 buttons i have on my page. Currently it is written out like this :

problem1buttonEl.addEventListener("click", problem1);
problem2buttonEl.addEventListener("click", problem2);
problem3buttonEl.addEventListener("click", problem3);
problem4buttonEl.addEventListener("click", problem4);

Now I want to make a for loop to make this cleaner, something like this:

for (var problemIncrement = 1; problemIncrement <= 30; problemIncrement++) {
'problem' + problemIncrement + 'buttonEl'.addEventListener("click", 
'problem' + problemIncrement);
}

The problem is that i need to add a string to my variable and have the outcome of them also be a variable, as that is what problemXbuttonEl is.

Is there any way to do this?

cнŝdk

You can use the brackets notation [] to get these variables, normally they should be defined in the window object so this is how should be your code:

for (var problemIncrement = 1; problemIncrement <= 30; problemIncrement++) {
    window['problem' + problemIncrement + 'buttonEl'].addEventListener("click", 
    window['problem' + problemIncrement]);
}

Here window['problem' + problemIncrement + 'buttonEl'] will refer to the varibale you already defined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I create a new table in my SQLite Database using a param from a Java String variable?

From Dev

How can I use multiple conditionals and match to create a new variable?

From Dev

How can I create a variable with a dynamic name based on a string in MATLAB?

From Dev

How can I use a loop index to reference variable names and create new variable names?

From Dev

How can I check if a file contains a string or a variable in JavaScript?

From Dev

I can't create a true variable in Javascript

From Dev

How can I combine a variable name when export a new variable?

From Dev

How can I combine a variable name when export a new variable?

From Dev

How can I create a Boxplot for one Variable?

From Dev

How can I create a variable that performs the setter of another variable?

From Dev

How can I create lag variable for a particular variable for each ID

From Dev

How can I call a variable from a string?

From Dev

How can I remove a variable that contains a string?

From Dev

How can I use variable in connection string?

From Dev

How can I convert my variable into a string?

From Dev

How can I concatenate a string with a variable and use it as another variable?

From Dev

How can I find a variable with prompt in javascript?

From Dev

Can I open a new window and populate it with a string variable?

From Dev

How can I use a variable's value in a variable name in JavaScript?

From Dev

Can I generate new array with another variable in javascript?

From Dev

Can I reassign a new array to an array variable in JavaScript?

From Dev

Javascript: How can I create a function that 1. accepts a global variable as a parameter 2. alters the value of that global variable?

From Dev

How combine one string and variable for new variable

From Dev

How combine a variable and a string to form a new variable?

From Dev

How do I combine form class values into new variable string

From Dev

Can I set a variable to a variable combination in javascript?

From Dev

Can I use a variable as variable name in JavaScript?

From Dev

Join an array object and string variable in javascript or angularjs

From Dev

can i turn string into a variable?

Related Related

  1. 1

    How can I create a new table in my SQLite Database using a param from a Java String variable?

  2. 2

    How can I use multiple conditionals and match to create a new variable?

  3. 3

    How can I create a variable with a dynamic name based on a string in MATLAB?

  4. 4

    How can I use a loop index to reference variable names and create new variable names?

  5. 5

    How can I check if a file contains a string or a variable in JavaScript?

  6. 6

    I can't create a true variable in Javascript

  7. 7

    How can I combine a variable name when export a new variable?

  8. 8

    How can I combine a variable name when export a new variable?

  9. 9

    How can I create a Boxplot for one Variable?

  10. 10

    How can I create a variable that performs the setter of another variable?

  11. 11

    How can I create lag variable for a particular variable for each ID

  12. 12

    How can I call a variable from a string?

  13. 13

    How can I remove a variable that contains a string?

  14. 14

    How can I use variable in connection string?

  15. 15

    How can I convert my variable into a string?

  16. 16

    How can I concatenate a string with a variable and use it as another variable?

  17. 17

    How can I find a variable with prompt in javascript?

  18. 18

    Can I open a new window and populate it with a string variable?

  19. 19

    How can I use a variable's value in a variable name in JavaScript?

  20. 20

    Can I generate new array with another variable in javascript?

  21. 21

    Can I reassign a new array to an array variable in JavaScript?

  22. 22

    Javascript: How can I create a function that 1. accepts a global variable as a parameter 2. alters the value of that global variable?

  23. 23

    How combine one string and variable for new variable

  24. 24

    How combine a variable and a string to form a new variable?

  25. 25

    How do I combine form class values into new variable string

  26. 26

    Can I set a variable to a variable combination in javascript?

  27. 27

    Can I use a variable as variable name in JavaScript?

  28. 28

    Join an array object and string variable in javascript or angularjs

  29. 29

    can i turn string into a variable?

HotTag

Archive