Ashton Compton Ashton Compton - 1 year ago 70
HTML Question

How could I delete a specific element that I have created in javascript?

I'm making something for my own use that will allow me to quickly and easily stack commands (for Minecraft command block creations).

I have already created a button to create new textareas and a button to delete them. Presuming that there will be several textareas created, how could I delete a specific textbox in the middle of all of them (with the button to delete them)?

I have a div element to act as the parent, and actually was able to successfully delete the textareas AND buttons. My problem is after deleting even just one, I wasn't able to create more. And I noticed the text in the boxes would shift to the left.

The function :

function removeBox() {
var div = document.getElementById("newText");
var cats = document.getElementsByClassName("tAC");
var catss = document.getElementsByClassName("tACB");

Don't judge me because I named the variables cats!
The div :

<div id="newText">
<textarea class="tAC" id="firstText"></textarea>

Any ideas?

Answer Source

With what you have posted, I am suggesting this.

Whenever a new textarea is created, create a new button within the div that holds the textarea. This way when the remove button is clicked, you can use to get the button element which dispatched the event and from there you can use to find the textarea and remove it from the DOM by calling removeChild on I am not sure if this is what you expect, so I didn't share code.