You can't use "
characters in an HTML attribute value which doesn't have delimiters and you are missing a )
.
You could add the missing bits:
sb.append("<a href='javascript:testFunction(\""+str+"\");'>test</a>");
But you would be better off:
- Not constructing JavaScript by mashing together strings
- Not constructing HTML by mashing together strings
- Not using a link to run JavaScript
Such:
var button = document.createElement('button');
button.type = "button";
button.appendChild(
document.createTextNode('test')
);
button.addEventListener('click', function (event) {
testFunction(str);
});
ab.append(button); // Assuming that whatever `ab.append` is accepts a DOM element