Why is the argument function running when the button is made? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Dec 2019, 11:33 PM
JJ McSquiggles
JJ McSquiggles - avatar
12 Answers
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
4th Dec 2019, 10:01 PM
JJ McSquiggles
JJ McSquiggles - avatar
+ 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?
4th Dec 2019, 3:17 PM
Jax
Jax - avatar
+ 1
Try wrapping it in an onload event: window.onload = function(){ btn("click me", say) }
5th Dec 2019, 12:12 AM
Jax
Jax - avatar
0
I believe you would simply need to remove the parentheses. So “click”, does() would become “click”, does.
3rd Dec 2019, 11:37 PM
Jax
Jax - avatar
0
Jax That leads to "Uncaught TypeError: append child cannot be null" I don't remember the exact verbage
4th Dec 2019, 2:38 AM
JJ McSquiggles
JJ McSquiggles - avatar
4th Dec 2019, 5:51 AM
Jax
Jax - avatar
0
Huh. I still give me that error. Maybe it has to do with the web browser or something?
4th Dec 2019, 2:28 PM
JJ McSquiggles
JJ McSquiggles - avatar
0
I’m not sure. What web browser are you using?
4th Dec 2019, 2:44 PM
Jax
Jax - avatar
0
Chrome
4th Dec 2019, 2:58 PM
JJ McSquiggles
JJ McSquiggles - avatar
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
4th Dec 2019, 9:39 PM
JJ McSquiggles
JJ McSquiggles - avatar
4th Dec 2019, 10:08 PM
JJ McSquiggles
JJ McSquiggles - avatar
0
YEET
5th Dec 2019, 8:26 AM
Thulean Perspective
Thulean Perspective - avatar