I want to grab an HTML element using a variable which contains a string which is also the name of my HTML element. When calling the variable containing the string I don´t want to get the string itself but the ID of the HTML element which has the same name. How can I do that?
In particular, I want to do this:
I´ve got some objects:
<canvas width="250" height="250" id="output_1"></canvas>
<canvas width="250" height="250" id="output_2"></canvas>
<canvas width="250" height="250" id="output_3"></canvas>
In the script I make a string which equals the ID:
var aktuellerCanvas;
var count = -1;
Later, I want to grab the elements by ID, first "output_1", then "output_2" and so on.... I tried this, but I was not successful. I guess, I´m only calling the string in the variable aktuellerCanvas.
count++;
aktuellerCanvas = "output_" + (count % 3 ++);
aktuellerCanvas.putImageData(image, 0, 0, 0, 0, width, height);
Can you help me?
aktuellerCanvasis just a string, not a html elementcount % 3 ++is not legal. You probably meant++count % 3.