+1

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

12/3/2019 11:33:26 PM

JJ McSquiggles

12 Answers

New Answer

0

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

+1

hmm. I’m using chrome as well, and there are no issues for me. Do you have another browser you could try it in?

+1

Try wrapping it in an onload event: window.onload = function(){ btn("click me", say) }

0

I believe you would simply need to remove the parentheses. So “click”, does() would become “click”, does.

0

Jax That leads to "Uncaught TypeError: append child cannot be null" I don't remember the exact verbage

0

This works for me https://code.sololearn.com/WWM7euzzLg5c/?ref=app

0

Huh. I still give me that error. Maybe it has to do with the web browser or something?

0

I’m not sure. What web browser are you using?

0

0

I tried your code in IE, Edge, and Chrome (both on a PC and my phone) and it works in none of them. What magic are you using? This is weird... Exact Error Text: Unable to get property 'appendChild' of undefined or null reference Line: 13

0

https://www.sololearn.com/Discuss/2088852/why-is-document-body-null-javascript New Post-Question

0