The method you're looking for is ChildNode.remove():
The ChildNode.remove() method removes the object from the tree it belongs to.
- https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
At the same time, the main problem that I see with your code thus far is that it lacks encapsulation. Encapsulation is a word that has a lot of specific meanings attributed to it but it generally means that instances should be protected from other instances so that you always know which instance you're working on and you don't have to "find" it.
The easiest way to do this when working on DOM projects like this is to use Custom Elements
One of the key features of the Web Components standard is the ability to create custom elements that encapsulate your functionality on an HTML page, rather than having to make do with a long, nested batch of elements that together provide a custom page feature.
- https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements
Below I've created a class that extends HTMLElement such as to implement an autonomous custom element. I've then separated the concerns, and implemented the click event handler for the .remove button.
Another thing to keep in mind is that id attributes must be unique within the document. This means that you should not be assigning an id attribute with the value remove to every remove button.
The id global attribute defines a unique identifier (ID) which must be unique in the whole document.
- https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
class ItemBox extends HTMLElement { constructor() { super() // This must be done after the element is created because // the element must not contain children upon creation setTimeout(_ => this.appendChildren(this.createChildren())) } createChildren() { return [ this.createRemoveButton(), this.createOutputContainer() ] } appendChildren(children) { for(let child of children) { this.appendChild(child) } } createRemoveButton() { const element = document.createElement('span') element.classList.add('remove') element.innerText = `\u2715` element.addEventListener('click', event => this.remove()) return element } createOutputContainer() { const element = document.createElement('p') element.classList.add('output') element.innerText = 'The text in the box' return element } } customElements.define('item-box', ItemBox) const boxContainer = document.getElementById('box_container') const createButton = document.getElementById('box_creator') createButton.addEventListener('click', event => { boxContainer.appendChild(document.createElement('item-box')) })
item-box { display: flex; flex-flow: row nowrap; background: #DDD; line-height: 1.5em; margin: 5px; } item-box .remove, item-box .output { padding: 5px; } item-box .remove { cursor: pointer; user-select: none; } item-box .output { margin: 0; }
<button id="box_creator">create box</button> <div id="box_container"></div>