-
Notifications
You must be signed in to change notification settings - Fork 23.1k
Make a single line of code-example easier to read and understand #42185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
For learners that never learned DOM, I guess `body.removeChild(panel)` is clearer than `panel.parentNode.removeChild(panel)`. And I guess it fits better with the previous line of code: `body.appendChild(panel)`.
files/en-us/learn_web_development/core/scripting/build_your_own_function/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
|
||
| Finally, we call {{domxref("EventTarget/addEventListener", "addEventListener()")}} to add a function that will be called when the user clicks the "close" button. The code will delete the whole panel from the page — to close the message box. | ||
|
|
||
| Briefly, the `addEventListener()` method is provided by the button (or in fact, any element on the page) that can be passed a function and the name of an event. In this case, the name of the event is 'click', meaning that when the user clicks the button, the function will run. You'll learn a lot more about events in our [events article](/en-US/docs/Learn_web_development/Core/Scripting/Events). The line inside the function uses the {{domxref("Node.removeChild()")}} DOM API function to specify that we want to remove a specific child element of the HTML element — in this case, the panel `<div>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @chrisdavidmills, I hope I am not over-calling you.
That sentence:
Briefly, the addEventListener() method is provided by the button (or in fact, any element on the page) that can be passed a function and the name of an event. In this case, the name of the event is 'click', meaning that when the user clicks the button, the function will run.
Looks a bit awkward to me.
Rephrasing it is outside of my scope. And if you think it can use some enhancement, please give it a shot.
|
Preview URLs Flaws (1)URL:
(comment last updated: 2025-12-01 16:32:41) |
chrisdavidmills
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @youssef-el-atmani!
This is quite interesting — I think I used panel.parentNode.removeChild(panel) as this is what we were forced to use before removeChild()/remove() were available. But I don't think I wrote it that long ago ;-)
In any case, yes, removeChild() is a better, more intuitive option.
I've got a couple of comments for you to look through.
files/en-us/learn_web_development/core/scripting/build_your_own_function/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Chris Mills <chrisdavidmills@gmail.com>
chrisdavidmills
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youssef-el-atmani awesome, I think this one is ready to go now. Approving.
Thanks again.
For learners that never learned DOM, I guess
body.removeChild(panel)is clearer thanpanel.parentNode.removeChild(panel).And I think it fits better with the previous line of code:
body.appendChild(panel).Description
Motivation
Additional details
Related issues and pull requests