Why is the argument function running when the button is made?
I have written the following code. The issue is that say() is running when I create the button (using btn) not when the button is clicked. JavaScript is the language. The following code will output this when run. The [Click me] is representing the button text[Click me] This is my code: function btn(text, does) { var button = document.createElement("button"); button.innerHTML = text; button.addEventListener("click" , does() );//end parameters document.body.appendChild(button); }//ends function btn function say() { document.write("text"); } btn("Click me", say ); : End Code Why is say() running immediately? I have tested and has something to do with the having the button. Is the eventListener not working? Is the issue document.write? Aliens? Thank you! EDIT: I have done some testing and googling and it seems that a) does should have no () since it is function that is being inputed as an argument to another function b) the error that I get when I do that is from this line: document.body.appendChild(button); The error (I think the exact verbage changes between web browsers tho): 'Cannot read property 'appendChild' of null" is saying it cannot read the property of an object if the object is null itself. The issues is that body (document.body) is not defined. I don't know why. Since the nature of my confusion has resulted in a completely different question I will create a new question-post that asks why document.body is null. Thanks Jax