In the code below, I am trying to set the text for my 4 buttons. Would appreciate a helping hand with:
allButtons[i].innerHTML = btn${i};
and particularly with
btn${i}
I am trying to pass to .innerHTML the text stored above, but so far failed to do it. Most attempts I tried led to:
Uncaught SyntaxError: Unexpected token {
Question: how can I interpolate a variable into a variable in JS? The code:
var btn0 = "Load tasks from server";
var btn1 = "Load tasks from Local Storage";
var btn2 = "Create tasks";
var btn3 = "Edit tasks";
var allButtons = document.getElementsByTagName('button');
function setText( btn0, btn1, btn2, btn3, allButtons ) {
var allButtons = document.getElementsByTagName('button');
for (i = 0; i < allButtons.length; i++) {
console.log(i);
allButtons[i].innerHTML = btn${i}; // interpolation issue
}
}