19

How can I remove the background-color and opacity property using Javascript only (no Jquery!).

I tried this:

document.getElementById('darkOverlay').style.removeProperty("background-color");
document.getElementById('darkOverlay').style.removeProperty("opacity");

but it did not work.

utdev
  • 3,942
  • 8
  • 40
  • 70

4 Answers4

40

You can just reset properties by either setting them to an empty string:

document.getElementById('darkOverlay').style.backgroundColor = "";
document.getElementById('darkOverlay').style.opacity = "";

Or setting them to the default values you like:

document.getElementById('darkOverlay').style.backgroundColor = "transparent";
document.getElementById('darkOverlay').style.opacity = "1";
andreas
  • 16,357
  • 12
  • 72
  • 76
4
document.getElementById("darkOverlay").removeAttribute("style");

Works fine for me... Works only if you put your opacity attribute and background in style

  • 2
    Wrong solution. It will remove other styling effects with background. –  Apr 22 '19 at 12:24
3

try

document.getElementById('darkOverlay').style.backgroundColor= 'transparent';
document.getElementById('darkOverlay').style.opacity= 1;
capo11
  • 790
  • 3
  • 11
  • 32
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
1

Try this:

var element = document.getElementById('darkOverlay');
element.style.backgroundColor = null;
element.style.opacity = null;
weinde
  • 1,074
  • 3
  • 13
  • 32
  • 2
    `element.style.background-color` is not a viable variable notation in Javascript. Use either `element.style.backgroundColor` or `element.style['background-color']`. – connexo Nov 02 '16 at 13:27