Está funcionando, pero cuando se hace clic en Cerrar icono, no se elimina
no se como lo hago!!! Porque estoy haciendo un cuadro de alerta personalizado y estoy haciendo esto
Creando div y no eliminado
Quiero cerrar/eliminar el elemento div y el div está en createElement(); Función
Y probé W3Schools y Stack Overflow Questions
Pero todavía no funciona
no sé por qué
Aquí está mi código:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Custom Alert Box</title> </head> <body> <button onclick="alert('hello')">Try it</button> <script> alert = function(arguments) { var alertbox = document.createElement("div"); var close = document.createElement("img"); var text = document.createTextNode(arguments); alertbox.setAttribute("style", ` border: 0.8px solid #002; border-radius: 4px; box-shadow: 0 2px 4px #454c4e; padding: 6px; height: 80px; ` ); close.setAttribute("style", ` width: 18px; height: auto; float: right; ` ); close.setAttribute("src", "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-close-512.png"); close.setAttribute("alt", "close"); close.setAttribute("onclick", alertbox.remove()); // The Close Icon That Removes/Closes The Div alertbox.appendChild(text); alertbox.appendChild(close) document.body.appendChild(alertbox); } </script> </body> </html>```debe agregar un eventListener en lugar de setattribute. en vez de :
close.setAttribute("onclick", alertbox.remove());usar :
close.addEventListener("click", () => { alertbox.remove();})Si entiendo bien tu pregunta, quieres eliminar un div del DOM.
document.querySelector()myelement.parentElementparent.removeChild() Otra solución sería ocultar el elemento una vez que se haya creado (con display: none propiedad CSS, por ejemplo).