I have a simple script that dynamically generates buttons. Clicking on a button generates an error message in the console: "Uncaught SyntaxError: Unexpected token ')' (at (index):1:8)". Why is this error being generated, and how do I prevent it?
<button onclick="delete();">A Pet</button>
delete is a keyword, and not valid as a function name. When the JS engine encounters delete() in the click handler body, it interprets it as a delete statement applied to (), which is an empty expression, and syntactically invalid.
To fix, use a non-keyword name. Note that it might be better to use addEventListener instead of an inline handler.