Skip to content

Conversation

@youssef-el-atmani
Copy link
Contributor

For learners that never learned DOM, I guess body.removeChild(panel) is clearer than panel.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

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)`.
@youssef-el-atmani youssef-el-atmani requested a review from a team as a code owner December 1, 2025 13:42
@youssef-el-atmani youssef-el-atmani requested review from estelle and removed request for a team December 1, 2025 13:42
@github-actions github-actions bot added Content:Learn Learning area docs size/xs [PR only] 0-5 LoC changed labels Dec 1, 2025
@github-actions github-actions bot added size/s [PR only] 6-50 LoC changed and removed size/xs [PR only] 0-5 LoC changed labels Dec 1, 2025
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>`.
Copy link
Contributor Author

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.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2025

Preview URLs

Flaws (1)

URL: /en-US/docs/Learn_web_development/Core/Scripting/Build_your_own_function
Title: Build your own function
Flaw count: 1

  • macros:
    • Can't resolve /en-US/curriculum/

(comment last updated: 2025-12-01 16:32:41)

Copy link
Contributor

@chrisdavidmills chrisdavidmills left a 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.

Co-authored-by: Chris Mills <chrisdavidmills@gmail.com>
Copy link
Contributor

@chrisdavidmills chrisdavidmills left a 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.

@chrisdavidmills chrisdavidmills merged commit 8d244a3 into mdn:main Dec 1, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:Learn Learning area docs size/s [PR only] 6-50 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants