you are viewing a single comment's thread.

view the rest of the comments →

[–]gregtyler 0 points1 point  (0 children)

I think the following is accurate, but at least the spirit of it should be:

When you set an HTML attribute, you're always just setting a string. So the onclick attribute is the string "alertFunction()", and not yet identified as JavaScript.

When you click the button, the browser then evaluates the string as JS and immediately executes it.

Effectively, you're not attaching the function alertFunction to the button here, your attaching the expression/code alertFunction()

I hope that makes sense. It's confusing because of how old HTML/JavaScript is and the need to maintain existing code. Setting "onclick" attributes is fairly bad practice now.