You can't embed a Function
instance inside a string.
If the buttons
variable is global, it will be accessible to code inside the onclick
event handler attribute:
var htmlbutton= '<button type="button" onclick="buttons[\''+buttonName+'\']()">'+buttonName+'</button>'
However, there is no escaping in the above, so you will have problems (potentially security problems) if buttonName
can ever contain a <
, &
, '
or "
character. You would have to escape these in various ways for the JavaScript and HTML you're embedding the strings in.
Much better is to avoid slinging strings to make HTML at all. Then you can use DOM methods and native JavaScript objects and avoid all the confusing nastiness of strings-inside-strings.
var button= document.createElement('button');
button.type= 'button';
button.appendChild(document.createTextNode(buttonName));
button.onclick= buttons[buttonsName];