Imagine this snippet of CSS code:
.notif-icon {
display: inline-block;
}
In my HTML, I have the following element which starts off hidden by overriding the display property of the notif-icon
class:
<span id="error" class="notif-icon" style="display:none">ERROR!</span>
I want to show the "ERROR!" message (when necessary) by removing the display property override. The question is how best to accomplish this. Any of these methods seem to work:
document.getElementById("error").removeAttribute("style");
document.getElementById("error").style.removeProperty("display");
However, I prefer working with element properties rather than calling attribute/property functions. I attempted these alternatives, but none were successful:
delete document.getElementById("error").style;
delete document.getElementById("error").style.display;
Yet, any of the following approaches do work:
-
document.getElementById("error").style = "";
-
document.getElementById("error").style = undefined;
-
document.getElementById("error").style.display = "";
Assigning undefined
to the display property does *not* achieve the desired effect.
So, my main question is which of the three effective assignments should be preferred, if any. Additional explanations or references on the different variants would also be welcome.