I’m trying to add a function call in an onclick event to the data being returned. I haven’t been able to figure out if I’m missing quotes anywhere. Could you provide any insight the correct to do this?
the_data = "<a onclick=popup_manager.getFormPopUp(" + options + ")>"+the_data+ "</a>";
3
Answers
Did you want something like this?
You need to wrap the value you use in the arguments of your function in quotes which are escaped from the body of the string which contains them:
Improving this approach, you could use a template literal to make the syntax less messy and avoid the need to escape the inner quotes:
To take this another step further, you could follow best practice and remove the outdated inline
onclick
attribute, which should be avoided, and instead attach your event handlers unobtrusively, providing the arguments to the handler function throughdata
attributes in your HTML:you can use document.createElement to get the result you wanted
then append ‘a’ to your html document