skip to Main Content

My website design came out good but the script didn’t execute. I couldn’t find the problem and tried reviewing my code but still nothing yet. Could you please help me? My last resorts all did not work out but when I checked the source it worked so I do not know what I did wrong. I tried removing and adding a few elements but for some reason the hide element didn’t work at all so I have no other way but to ask for advice in here.

Here’s the code:

let schools = {
  data: [{
      schoolName: "Edmonton Catholic School District",
      category: "Catholic",
      image: "ecsd.png"
    },
    {
      schoolName: "Edmonton Public School",
      category: "Regular",
      image: "publicimg.jpg"
    },
    {
      schoolName: "Northern Alberta Institution of Technology",
      category: "College",
      image: "NAIT.png"
    },
    {
      schoolName: "University of Alberta",
      category: "University",
      image: "University-of-Alberta.png"
    },
  ],
};

for (let i of schools.data) {
  //Create Card
  let card = document.createElement("div");

  //Card category and should be hidden
  card.classList.add("card", i.category, "hide");

  //Image div
  let imgContainer = document.createElement("div");
  imgContainer.classList.add("image-container");

  //img tag
  let image = document.createElement("img");
  image.setAttribute("src", i.image);
  imgContainer.appendChild(image);
  card.appendChild(imgContainer);

  //container
  let container = document.createElement("div");
  container.classList.add("container");

  //School name
  let name = document.createElement("h5");
  name.classList.add("school-name");
  name.innerText = i.schoolName.toUpperCase();
  container.appendChild(name);

  card.appendChild(container);
  document.getElementById("schools").appendChild(card);
}

function filterSchool(value) {

  let elements = document.querySelectorAll(".card");
  elements.forEach((element) => {
    if (value == "all") {
      element.classList.remove("hide");
    } else {
      if (element.classList.contains(value)) {
        element.classList.remove("hide");
      } else {
        element.classList.add("hide");
      }
    }
  });
}

window.onload = () => {
  filterSchool("all");
}
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  border: none;
  outline: none;
  font-family: 'Poppins', sans-serif;
}

body {
  background-color: white;
}

button {
  cursor: pointer;
}

.wrapper {
  position: absolute;
  top: 5%;
  left: 20%;
  width: 95%;
  margin: 0 auto;
}

.button-value {
  border: 2px solid black;
  padding: 0.5px 10px;
  border-radius: 3em;
  background-color: transparent;
  transition: 0.3s ease-in-out;
}

.button-value:focus {
  background-color: black;
  color: white;
}

#schools {
  display: grid;
  grid-template-columns: auto auto auto;
  grid-column-gap: 1.5em;
  padding: 2em 0;
}

.card {
  background-color: white;
  max-width: 18em;
  margin-top: 1em;
  padding: 1em;
  border-radius: 5px;
  box-shadow: 1em 2em 2.5em rgba(0, 0, 0, 0.19);
}

.image-container {
  text-align: center;
}

img {
  max-width: 100%;
  object-fit: contain;
  height: 15em;
}

.container {
  padding-top: 1em;
  color: black;
}

@media screen and (max-width: 720px) {
  img {
    max-width: 100%;
    object-fit: contain;
    height: 10em;
  }
  .card {
    max-width: 10em;
    margin-top: 1em;
  }
  #schools {
    grid-template-columns: auto auto;
    grid-column-gap: 1em;
  }
}
<div class="wrapper">
  <div id="buttons">
    <button class="button-value" onclick="filterSchool('all')">All</button>
    <button class="button-value" onclick="filterSchool('Catholic')">Catholic School</button>
    <button class="button-value" onclick="filterSchool('Regular')">Regular School</button>
    <button class="button-value" onclick="filterSchool('University')">University</button>
    <button class="button-value" onclick="filterSchool('College')">College</button>
  </div>
  <div id="schools"></div>
</div>

2

Answers


  1. The error is

     Error: {
          "message": "Uncaught ReferenceError: windows is not defined",
          "filename": "https://stacksnippets.net/js",
          "lineno": 175,
          "colno": 1
        }
    

    The reason why it shows because windows is not a function the correct is window

    Login or Signup to reply.
  2. The filter function adds a class called hide to each card that doesn’t meet the selection critiera. The thing is you’ve not defined what this class does. If you add

    .hide { display: none; }

    to your css it’ll work. Working example below using ‘College’ as a filter.

     let schools = {
    data: [{
        schoolName: "Edmonton Catholic School District",
        category: "Catholic",
        image: "ecsd.png"
      },
      {
        schoolName: "Edmonton Public School",
        category: "Regular",
        image: "publicimg.jpg"
      },
      {
        schoolName: "Northern Alberta Institution of Technology",
        category: "College",
        image: "NAIT.png"
      },
      {
        schoolName: "University of Alberta",
        category: "University",
        image: "University-of-Alberta.png"
      },
    ],
    };
    
    for (let i of schools.data) {
    //Create Card
    let card = document.createElement("div");
    
    //Card category and should be hidden
    card.classList.add("card", i.category, "hide");
    
    //Image div
    let imgContainer = document.createElement("div");
    imgContainer.classList.add("image-container");
    
    //img tag
    let image = document.createElement("img");
    image.setAttribute("src", i.image);
    imgContainer.appendChild(image);
    card.appendChild(imgContainer);
    
    //container
    let container = document.createElement("div");
    container.classList.add("container");
    
    //School name
    let name = document.createElement("h5");
    name.classList.add("school-name");
    name.innerText = i.schoolName.toUpperCase();
    container.appendChild(name);
    
    card.appendChild(container);
    document.getElementById("schools").appendChild(card);
    }
    
    function filterSchool(value) {
    
    let elements = document.querySelectorAll(".card");
    elements.forEach((element) => {
      if (value == "all") {
        element.classList.remove("hide");
      } else {
        if (element.classList.contains(value)) {
          element.classList.remove("hide");
        } else {
          element.classList.add("hide");
        }
      }
    });
    }
    
    window.onload = () => {
    filterSchool("College");
    }
        * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      border: none;
      outline: none;
      font-family: 'Poppins', sans-serif;
    }
    
    body {
      background-color: white;
    }
    
    button {
      cursor: pointer;
    }
    
    .wrapper {
      position: absolute;
      top: 5%;
      left: 20%;
      width: 95%;
      margin: 0 auto;
    }
    
    .button-value {
      border: 2px solid black;
      padding: 0.5px 10px;
      border-radius: 3em;
      background-color: transparent;
      transition: 0.3s ease-in-out;
    }
    
    .button-value:focus {
      background-color: black;
      color: white;
    }
    
    #schools {
      display: grid;
      grid-template-columns: auto auto auto;
      grid-column-gap: 1.5em;
      padding: 2em 0;
    }
    
    .card {
      background-color: white;
      max-width: 18em;
      margin-top: 1em;
      padding: 1em;
      border-radius: 5px;
      box-shadow: 1em 2em 2.5em rgba(0, 0, 0, 0.19);
    }
    
    .image-container {
      text-align: center;
    }
    
    img {
      max-width: 100%;
      object-fit: contain;
      height: 15em;
    }
    
    .container {
      padding-top: 1em;
      color: black;
    }
    
    /* added this */
    .hide {
      display:none;
    }
    
    @media screen and (max-width: 720px) {
      img {
        max-width: 100%;
        object-fit: contain;
        height: 10em;
      }
      .card {
        max-width: 10em;
        margin-top: 1em;
      }
      #schools {
        grid-template-columns: auto auto;
        grid-column-gap: 1em;
      }
    }
      <div class="wrapper">
        <div id="buttons">
          <button class="button-value" onclick="filterSchool('all')">All</button>
          <button class="button-value" onclick="filterSchool('Catholic')">Catholic School</button>
          <button class="button-value" onclick="filterSchool('Regular')">Regular School</button>
          <button class="button-value" onclick="filterSchool('University')">University</button>
          <button class="button-value" onclick="filterSchool('College')">College</button>
        </div>
        <div id="schools"></div>
      </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search