Home > Net >  How can we set background image none with javascript?
How can we set background image none with javascript?

Time:10-28

I can't figure out I want when I press button set backgroundImage = none but I cant I was trying this my code;

My section

var colorChanger = document.getElementsByTagName("button")[0];
colorChanger.addEventListener("click", function() {
  document.body.style.backgroundColor = "#000";
  document.getElementById("home").style.backgroundImage = "none";
#home {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  min-height: 100vh;
  align-items: center;
  background-image: url(https://via.placeholder.com/200.png);
  background-size: cover;
  background-position: center;
<section class="home" id="home">
  <div class="content">
    <h3>Who I am?</h3>
    <p>
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
      survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      <h2>My Education Process</h2>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
        It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
        desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </p>
      <a href="About.html" class="btn">Read More...</a> <br><br></section>
</p>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

also I was trying;

var colorChanger = document.getElementsByTagName("button")[0];
colorChanger.addEventListener("click",function() {
  document.body.style.backgroundColor = "#000"; 
  document.getElementById("home").style.backgroundImage = null;

both don't work.
where am i doing wrong?

CodePudding user response:

  1. I added a class #home.bg-none that has a heigher specificty weight then a normal ID. Otherwise we would have a specificty weight issue because an ID has a heigher weight then a class.
  2. I added the propery and value background-image: none; to that class aswell as your intended background-color change.
  3. I used in JS: classList.add('class-name') to apply the class to the section on a button click.

function bgChange() {
  var sectionHome = document.querySelector('#home'); 
  sectionHome.classList.add('bg-none');
}
/* css changes */
#home.bg-none {
  background-image: none;
  background-color: #000;
  color: white;
}



/* original CSS, changed ID to class */
#home {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  min-height: 100vh;
  align-items: center;
  background-image: url(https://via.placeholder.com/200.png);
  background-size: cover;
  background-position: center;
}


/* for demonstrations purpose only */
body {
  margin: 0;
}

#home {
  justify-content: center;
  font-size: 3em;
}

button {
  position: fixed;
  top: 10px;
  left: 10px;
  background-color: grey;
}
<section id="home" class="home">Test-Section</section>

<button onclick="bgChange()">Click me</button>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use removeAttribute() and setAttribute() here is an example for you

var colorChanger = document.getElementsByTagName("button")[0];

colorChanger.onclick = function(){
document.getElementById("home").removeAttribute("class");
document.getElementById("home").setAttribute("class", "homecss"); 
}

CodePudding user response:

function removeBG(){
              event.preventDefault();

            document.getElementById("home").style.background = "none";
        }
#home{
          display: flex;
          flex-wrap: wrap;
          gap:1.5rem;
          min-height: 100vh;
          align-items: center;
          background-image: url(https://lh3.googleusercontent.com/proxy/YID679VxCfxmJqd8YiF-E43a5XxxTC7R3dE5iGScHPUbMnDvG9-qAG1PEp9Qrl09pmQIzujGCEIpyAyQb25DWosqSTXPoRGcwihJ3wf-NCk4Mhunlwod-EOsoiRMgNuvzh7_if3sfitCBi4LBoVP8qPB);
          
          background-size: cover;
          background-position: center;
        }
<section class="home" id="home"  > <div class="content">
    <h3>Who I am?</h3>
    <p>
       Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        <h2>My Education Process</h2>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </p>
        <a href="about.html" class="btn" onclick="removeBG()">Read More...</a> <br><br></section>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related