0

I'm working on/struggling with some Javascript for a project. Currently I have only an index.html, which contains five sections, each with their own id, and each with a different background-image allocated to these sections.

I have a button in the footer, which when clicked, should change each of these section background-images to a different image.

The current background-image for #landingImage is DSC3719.jpg and I would want it to change to DSC7317.jpg, when I click the radio button.

Having watched hours of tutorial videos, I'm no further forward on how I should bring this idea together. The most recent tutorial I've watched has suggested using an if/else, with an alternative class for the background-image to be switched to. With this method in mind, I would have to use multiple classes, as the alternative image is different for each section. Is this correct?

I'm not looking for the code to be written for me, but a process on how to approach this issue.

I'd be grateful for any guidance!

Cheers.

#landingImage{
  background-image: url(../img/DSC3719.jpg);
  min-width:100%;
  min-height: 700px;
  position:relative;
  background-attachment:fixed;
  background-position:center;
  background-size:cover;
  background-repeat:no-repeat;
}
 <div>
     <input class="switch" type="checkbox" value="" id="footerBtn">
     <label class="switch-btn" for="footerBtn"></label>
 </div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Are you want custom radio button using javascript and css. this might helps https://ej2.syncfusion.com/documentation/radio-button/how-to/customize-radiobutton-appearance/ – Rayees AC Sep 23 '20 at 11:19
  • @RayeesAC _"I'm ... looking for ... a process on how to approach this issue."_ - How does it help OP to use a library for this? – Andreas Sep 23 '20 at 11:22
  • @Andreas He actually didn't understand the question – Simone Rossaini Sep 23 '20 at 11:30

4 Answers4

2

You can do this with CSS using the :checked pseudo class and a sibling combinator.

#landingImage{
  background-image: url("https://via.placeholder.com/150/0000FF/808080");
  min-width:100%;
  min-height: 700px;
  position:relative;
  background-attachment:fixed;
  background-position:center;
  background-size:cover;
  background-repeat:no-repeat;
}

.switch:checked ~ #landingImage {
  background-image: url("https://via.placeholder.com/150/FF0000/FFFFFF");
}
<div>
    <input class="switch" type="checkbox" value="" id="footerBtn">
    <label class="switch-btn" for="footerBtn"></label>
    
    <div id="landingImage"></div>
 </div>

As commented, the CSS solution may become cumbersome with multiple images to change.

To implement this in javascript you can store the intended alternate image src in a data attribute in the HTML and then iterate over the relevant elements on checkbox change.

HTML5 is designed with extensibility in mind for data that should be associated with a particular element but need not have any defined meaning. data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, extra properties on DOM, or Node.setUserData().

Using data attributes

function toggleImages() {
  // get all elements with class 'alt-image'
  const altImages = document.querySelectorAll('.alt-image');
  // iterate over the resulting NodeList
  altImages.forEach(image => {
    // check if the image has been switched
    if (!image.classList.contains('alt-active')) {
      // if not, get the alt-src from the data attribute
      const altsrc = image.getAttribute('data-alt-src');
      // set the alt url on the element
      image.style.backgroundImage = `url("${altsrc}")`;
      // add a class indicating it has been toggled
      image.classList.add('alt-active');
    } else {
      // if so, set the url to '' (the src specified in the CSS will be used)
      image.style.backgroundImage = '';
      // remove the active class
      image.classList.remove('alt-active');
    }
  });
}

// query the document for the toggle switch
const toggle = document.querySelector('.switch');

// add onchange listener to the toggle
toggle.addEventListener('change', toggleImages, false);
.container {
  display: flex;
  justify-content: space-between;
}
.alt-image {
  height: 40vh;
  width: 20vw;
  padding: 16px;
  position:relative;
  background-attachment:fixed;
  background-position:center;
  background-size:cover;
  background-repeat:no-repeat;
}

#landing1{
  background-image: url("https://via.placeholder.com/150/000F0F");
}
 #landing2{
  background-image: url("https://via.placeholder.com/150/FFFF00");
}
#landing3{
  background-image: url("https://via.placeholder.com/150/FF00F8");
}
#landing4{
  background-image: url("https://via.placeholder.com/150/808080");
}
<div class="container">
<div id="landing1" class="alt-image" data-alt-src="https://via.placeholder.com/150/0000FF"></div>
<div id="landing2" class="alt-image" data-alt-src="https://via.placeholder.com/150/FF0000"></div>
<div id="landing3" class="alt-image" data-alt-src="https://via.placeholder.com/150/FFFF00"></div>
<div id="landing4" class="alt-image" data-alt-src="https://via.placeholder.com/150/000000"></div>
</div>
<div>
    <input class="switch" type="checkbox" value="" id="footerBtn">
    <label class="switch-btn" for="footerBtn"></label>
 </div>
pilchard
  • 12,414
  • 5
  • 11
  • 23
0

As you can see in my example i add onChange for use my function everytime checkbox is checked, and simple change background.

function ChangeBG(checkbox){
  const landingImage = document.getElementById('landingImage');
  if(checkbox.checked){
    landingImage.style.backgroundImage = "url('https://via.placeholder.com/150')";
  }else{
    landingImage.style.backgroundImage = "url('../img/DSC3719.jpg')";
  }
}
#landingImage{
  background-image: url(../img/DSC3719.jpg);
  min-width:100%;
  min-height: 700px;
  position:relative;
  background-attachment:fixed;
  background-position:center;
  background-size:cover;
  background-repeat:no-repeat;
}
<div>
     <input class="switch" type="checkbox" value="" id="footerBtn" onChange="ChangeBG(this);">
     <label class="switch-btn" for="footerBtn"></label>
 </div>
 
 
 <div id="landingImage"></div>
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
0

The missing step could be:

  1. You need to bind an event handler to the checkbox. Very easy is the onclick event: https://www.w3schools.com/howto/howto_js_display_checkbox_text.asp
  2. You need to change the background image: change background image in body (The first answer uses jQuery, scroll down for vanilla JS)
PotatoesFall
  • 345
  • 2
  • 7
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – mplungjan Sep 23 '20 at 11:43
  • 1
    Thank you @PotatoesFall for the link! I suppose what I didn't include in my initial question was the idea of using eventHandlers as opposed to onclick, which was also suggested by another user. By all means, very grateful for your input! –  Sep 23 '20 at 12:16
0

...contains five sections, each with their own id, and each with a different background-image allocated to these sections.
I have a button in the footer, which when clicked, should change each of these section background-images to a different image.

How about toggling classes?

Here is how to change multiple images

In this SPECIFIC case, the images are all defined in the CSS. It is possible to use data-attributes, but in this case, I would prefer to keep all the image URLs in one place

Note I destruct the querySelectorAll since not all newer browsers support the forEach on an html collection

document.getElementById("footerBtn").addEventListener("click",function() {
  [...document.querySelectorAll(".coverImage")]
    .forEach(img => img.classList.toggle("checked",this.checked))
})
.coverImage {
  min-width: 100%;
  min-height: 700px;
  position: relative;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
#landingImage {
  background-image: url(https://via.placeholder.com/700?text=landing1.jpg);
}
#landingImage.checked {
  background-image: url(https://via.placeholder.com/700?text=landing2.jpg);
}

#otherImage {
  background-image: url(https://via.placeholder.com/700?text=other1.jpg);
}
#otherImage.checked {
  background-image: url(https://via.placeholder.com/700?text=other2.jpg);
}
<div>
  <input class="switch" type="checkbox" value="" id="footerBtn">
  <label class="switch-btn" for="footerBtn"></label>
</div>

<div id="landingImage" class="coverImage"></div>
<div id="otherImage" class="coverImage"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236