In JS, I am creating a link where the href attribute runs a function. The function simple opens an alert window with a given variable.
function alerter(str){
alert(str);
}
When I create the link, I use the following code.
var link = document.createElement('a');
link.setAttribute('href','javascript:alerter("hi")');
link.innerHTML = "Sauce";
If I do that, it works. However, what I want to do is alert a previously defined variable. But when i use
link.setAttribute('href','javascript:alerter(myvar)');
the popup no longer works. Also, is there a way to simply embed the alert in the href instead of writing a separate function?