I am attempting to utilize the appendChild method in JavaScript to generate a hyperlink that includes an onClick attribute. Unfortunately, I am struggling to get it to function properly or locate clear instructions on how to accomplish this straightforward task.
var link = document.createElement("a");
link.appendChild(document.createTextNode("Link"));
link.href = '#';
link.onclick = 'loadScript()';
document.body.appendChild(link);