My custom alert displays correctly but I can't work out how to close it after clicking the OK button.
JavaScript:
function showAlert2(message) {
// display a message with OK button
const alert2 = document.createElement("div")
alert2.textContent = message
alert2.classList.add("alert2")
alertContainer2.prepend(alert2)
// Create the OK button
ok_button_tag = document.createElement("button");
ok_button_text = document.createTextNode("OK")
ok_button = alert2.appendChild(ok_button_tag);
ok_button.className = "closebtn";
ok_button.appendChild(ok_button_text);
// Add an event listener to close the custom alert
ok_button.addEventListener("click", function () {
removeAlert2();
}, false);
}
function removeAlert2() {
alert("Hello!"); //this displays so the function is being called but nothing I have tried will close it
//following code or similar does not work
HTML_body = document.querySelector("body");
alert_container = document.getElementById("alertContainer2");
HTML_body.removeChild(alert_container);
}
Last edited by a moderator: