Setting an elements onclick attribute to a function with the value of a variable

  dom, javascript, onclick

I have something like this

<button id="a"></button>

var functionName = "b";
function b() {
  //code
}

Can I set the onclick attribute of element a to the function b using the variable functionName?

Sorry if this is worded or formatted weirdly, this is the first question I’ve asked on here and I have no idea how it works.

Source: Ask Javascript Questions

LEAVE A COMMENT