2

My HTML code has the following structure:

<a class='card'...<a/>
<div id = "divone" class="card__background" ... </div>

with four respective cards.

When one of the divs with class='card' is clicked. The JavaScript code is supposed to hide the 'class="card__background" inside the other class='card' divs. I already have a script code that doesn't work.

Why would that be?

I am looking to fix my JavaScript code to make it work

function imagechange(divid) {
  var x = document.getElementById(divid);
  if (x == "divone") {
    x.style.display = "block";
    divtwo.style.display = 'none';
    divthree.style.display = 'none';
    divfour.style.display = 'none';
  } else if (x == "divtwo";) {
    x.style.display = "block";
    divone.style.display = 'none';
    divthree.style.display = 'none';
    divfour.style.display = 'none';
  } else if (x == "divthree";) {
    x.style.display = "block";
    divone.style.display = 'none';
    divtwo.style.display = 'none';
    divfour.style.display = 'none';
  } else {
    x.style.display = "block";
    divone.style.display = 'none';
    divtwo.style.display = 'none';
    divthree.style.display = 'none';
  }
}
:root {
  --background-dark: #2d3548;
  --text-light: rgba(255, 255, 255, 0.6);
  --text-lighter: rgba(255, 255, 255, 0.9);
  --spacing-s: 8px;
  --spacing-m: 16px;
  --spacing-l: 24px;
  --spacing-xl: 32px;
  --spacing-xxl: 64px;
  --width-container: 1200px;
}

* {
  border: 0;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  height: 100%;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
}

body {
  height: 100%;
}

.hero-section {
  align-items: flex-start;
  background-image: linear-gradient(15deg, #0f4667 0%, #2a6973 150%);
  display: flex;
  min-height: 100%;
  justify-content: center;
  padding: var(--spacing-xxl) var(--spacing-l);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  grid-column-gap: var(--spacing-l);
  grid-row-gap: var(--spacing-l);
  max-width: var(--width-container);
  width: 100%;
}

@media(min-width: 540px) {
  .card-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media(min-width: 960px) {
  .card-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

.card {
  list-style: none;
  position: relative;
}

.card:before {
  content: '';
  display: block;
  padding-bottom: 150%;
  width: 100%;
}

.card__background {
  background-size: cover;
  background-position: center;
  border-radius: var(--spacing-l);
  bottom: 0;
  filter: brightness(0.75) saturate(1.2) contrast(0.85);
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  transform-origin: center;
  trsnsform: scale(1) translateZ(0);
  transition: filter 200ms linear, transform 200ms linear;
}

.card:hover .card__background {
  transform: scale(1.05) translateZ(0);
}

.card-grid:hover>.card:not(:hover) .card__background {
  filter: brightness(0.5) saturate(0) contrast(1.2) blur(20px);
}

.card__content {
  left: 0;
  padding: var(--spacing-l);
  position: absolute;
  top: 0;
}

.card__category {
  color: var(--text-light);
  font-size: 0.9rem;
  margin-bottom: var(--spacing-s);
  text-transform: uppercase;
}

.card__heading {
  color: var(--text-lighter);
  font-size: 1.9rem;
  text-shadow: 2px 2px 20px rgba(0, 0, 0, 0.2);
  line-height: 1.4;
  word-spacing: 100vw;
}
<body>
  <center>
    <h1>My Favorite Things</h1>
    <h2>Click on one to get started</h2>
  </center>
  <section class="hero-section">
    <div class="card-grid">
      <a class="card" onclick="imagechange('divone')" href="#">
        <div id="divone" class="card__background" style="background-image: url(photo-one.jpg)"></div>
        <div class="card__content">
          <p class="card__category">FAV #1</p>
          <h3 class="card__heading">Photo</h3>
        </div>
      </a>

      <a class="card" onclick="imagechange('divtwo')" href="#">
        <div id="divtwo" class="card__background" style="background-image: url(photo-two.jpg)"></div>
        <div class="card__content">
          <p class="card__category">FAV #2</p>
          <h3 class="card__heading">Drawing</h3>
        </div>
      </a>

      <a class="card" onclick="imagechange('divthree')" href="#">
        <div id="divthree" class="card__background" style="background-image: url(photo-three.jpg)"></div>
        <div class="card__content">
          <p class="card__category">FAV #3</p>
          <h3 class="card__heading">Sports & Lifting</h3>
        </div>
      </a>

      <a class="card" onclick="imagechange('divfour')" href="#">
        <div id="divfour" class="card__background" style="background-image: url(photo-four.jpg)"></div>
        <div class="card__content">
          <p class="card__category">FAV #4</p>
          <h3 class="card__heading">Anime & Peaky Blinders</h3>
        </div>
      </a>
      <div>
  </section>

</body>

2 Answers 2

1

When you use var x = document.getElementById(divid); you are getting an object. So your condition never going to be true.

If you want to check the id just change condition to:

function imagechange(divid) {
  const x = document.getElementById(divid)
  const id = x.id

  if (id === 'divone') {
    x.style.display = 'block'
    divtwo.style.display = 'none'
    divthree.style.display = 'none'
    divfour.style.display = 'none'
  } 
  else if (id === 'divtwo') {
    x.style.display = 'block'
    divone.style.display = 'none'
    divthree.style.display = 'none'
    divfour.style.display = 'none'
  }  
  else if (id === 'divthree') {
    x.style.display = 'block'
    divone.style.display = 'none'
    divtwo.style.display = 'none'
    divfour.style.display = 'none'
  }  
  else {
    x.style.display = 'block'
    divone.style.display = 'none'
    divtwo.style.display = 'none'
    divthree.style.display = 'none'
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

const id = x.id makes no sense. You already have the id, it's divid.
I am trying to explain why original x is not an string, it is only an example. Passing id by param when u have the event with the target is better but i am not making a refactor.
1

1 - Please avoid using scripts straight in your HTML, this leads to unorganized code, with a horrible structure, and pessible for maintence.

2 - I deleted the code in the script tags and in place put

<script src="./site.js"></script> 

In the same folder. Create a js file correspondent to the name of your html, in this case I used 'site.js'.

3- Use this code in your 'site.js' file.

var cards = ["card1", "card2", "card3", "card4"]
var divs = ["div1", "div2", "div3", "div4"]

cards.forEach(function (card, i){
    document.getElementById(card).addEventListener('click', function(){
    document.getElementById(divs[i]).style.display = 'block'
    for (let j in divs){
        if (i != j)  document.getElementById(divs[j]).style.display = 'none'
    }
  })
})

4 - Delete all the onclick="imagechange()" of your card class tags and add id for them.

5 - Your HTML will be like this:

<body>
  <center> 
   <h1>My Favorite Things</h1>
   <h2>Click on one to get started</h2>
  </center>
<section class="hero-section">
 <div class="card-grid">
   <a id = "card1" class="card" href="#">
     <div id = "div1" class="card__background" style="background-image: url(photo-one.jpg)"></div>
     <div class="card__content">
       <p class="card__category">FAV #1</p>
       <h3 class="card__heading">Photo</h3>
     </div>
   </a>
   
   <a id = "card2" class="card" href="#">
     <div id = "div2" class="card__background" style="background-image: url(photo-two.jpg)"></div>
     <div class="card__content">
       <p class="card__category">FAV #2</p>
       <h3 class="card__heading">Drawing</h3>
     </div>
   </a>
   
   <a id = "card3" class="card" href="#">
     <div id = "div3" class="card__background" style="background-image: url(photo-three.jpg)"></div>
     <div class="card__content">
       <p class="card__category">FAV #3</p>
       <h3 class="card__heading">Sports & Lifting</h3>
     </div>
   </a>
   
   <a id = "card4" class="card" href="#">
     <div id = "div4" class="card__background" style="background-image: url(photo-four.jpg)"></div>
     <div class="card__content">
       <p class="card__category">FAV #4</p>
       <h3 class="card__heading">Anime & Peaky Blinders</h3>
     </div>
   </a>
 <div>
</section>
<script src="./site.js"></script>
</body>

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.