0

I have a string I am using to get a pointer to my function. Then I want to use this function to set an onclick while passing arguments to the function.

var functionPtr = window[stringFunction];

pTag.onclick = function () { 
  functionPtr(args); 
};

When I do this, the onclick event tries to call "functionPtr(args)" instead of calling the function I'm pointing to. Using break points, I can see that functionPtr is definitely a reference to the function I want when I assign the onclick function. What steps am I missing here?

Thanks in advance!

Macros185
  • 231
  • 1
  • 3
  • 9

1 Answers1

1

I couldn't get my above attempt to work so I decided to set the onclick event using the setattribute() function instead.

var stringFunction = 'myFunction(args)';
pTag.setAttribute('onclick', stringFunction);

This seems to do the trick. Its important to note this doesn't work for earlier versions of IE, but I'm only required to work with IE 10 and up so this works for me.

Macros185
  • 231
  • 1
  • 3
  • 9