I have a situation where I need to create an onclick event that performs multiple tasks.
There is a button labeled Add Item in the image below. When clicked, it should add the HTML code shown below:
var addItemHTML = '<tr class="hold-item">\
<td><strong>'+typeField.value+': </strong></td>\
<td><input type="text"></td>\
<td><img class="cancel-item" src="icon/cancel.png" height="25px"></td>\
</tr>';
Everything works fine up to this point, but now I want to be able to remove the added HTML elements one by one by clicking on each close icon, and I'm having trouble achieving this!
I've attempted to use the following JavaScript code:
var cancelIcon = document.getElementsByClassName("cancel-icon");
var holdElement = document.getElementsByClassName("hold-element");
for(i=0;i < holdElement.length;i++){
cancelIcon[i].onclick = function(){
holdElement[i].remove();
}
}
Unfortunately, it doesn't seem to work. Can someone help me solve this issue?