7

I use in my webpage the css description

    body {
    font-size: 12px;
    font-family: Sans-Serif;
    background: url(images/diffdummy.png) no-repeat center center fixed;
    overflow: hidden;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

How I can remove the background image with java script on runtime? I tried

document.getElementById("html").style.background="";
document.getElementsByTagName("html").style.background="";
document.getElementsByTagName("html")[0].style.background="";

But nothing is working. Anybody here who can give me a hint?

2
  • document.body.style.background = 'none' Commented Jan 28, 2019 at 18:16
  • Class is set on the body, but you are selecting the html element? Commented Jan 28, 2019 at 18:20

2 Answers 2

16

Why html when you are using body as your CSS selector?

just use:

document.getElementsByTagName('body')[0].style.background = "none";

or

document.body.style.background = "none";

Code in action!

// removing background-color from body
document.getElementsByTagName('body')[0].style.background = "none";
body {
  height: 100px;
  width: 100%;
  background-color: red;
}

div {
  height: 40px;
  width: 20%;
  background-color: green;
}
<body>
 <div></div>
</body>

Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks, this document.body.style.background = "none"; works. I know the body was confusing but I tried both so sorry for this confusion.
@Ingo Its fine bro, we all make beginner mistakes when we started as well.
0

just to remove the background image should do like:

document.body.style.backgroundImage = "none";

to avoid i.e. removing background-color or other css props that might be applied to just background

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.