Build a Set of Football Team Cards - Build a Set of Football Team Cards

Tell us what’s happening:

I’ve been working on this project for a week now and i can’t pass test’s 11 through 15 and. Can’t figure out why. Any help will be appreciated.
Thanks in advance

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>
      Build a Set of Football Team Cards
    </title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1 class="title">Team stats</h1>
    <main>
      <div class="team-stats">
        <p>Team: <span id="team"></span></p>
        <p>Year: <span id="year"></span></p>
        <p>Head coach: <span id="head-coach"></span></p>
      </div>
      <label class="options-label" for="players">Filter Teammates:</label>
      <select name="players" id="players">
        <option value="all">All Players</option>
        <option value="forward">Position Forward</option>
        <option value="midfielder">Position Midfielder</option>
        <option value="defender">Position Defender</option>
        <option value="goalkeeper">Position Goalkeeper</option>
      </select>
      <div class="cards" id="player-cards"></div>
    </main>
    <footer>&copy; freeCodeCamp</footer>
    <script src="./script.js"></script>
  </body>
</html>

/* file: styles.css */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --dark-grey: #0a0a23;
  --light-grey: #f5f6f7;
  --white: #ffffff;
  --black: #000;
}

body {
  background-color: var(--dark-grey);
  text-align: center;
  padding: 10px;
}

.title,
.options-label,
.team-stats,
footer {
  color: var(--white);
}

.title {
  margin: 1.3rem 0;
}

.team-stats {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  font-size: 1.3rem;
  margin: 1.2rem 0;
}

.options-label {
  font-size: 1.2rem;
}

.cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.player-card {
  background-color: var(--light-grey);
  padding: 1.3rem;
  margin: 1.2rem;
  width: 300px;
  border-radius: 15px;
}

@media (max-width: 768px) {
  .team-stats {
    flex-direction: column;
  }
}

/* file: script.js */
	 //declare footballTeam variable/object
const footballTeam = {
  team: "Sunken Sailors",
  year: 2025,
  headCoach: "Popeye the sailorman",
  players: [
    {
    name: "Skipper",
    position: "forward",
    isCaptain: true,
  },
  {
    name: "Gilligan",
    position: "midfielder",
    isCaptain: false,
  },
  {
    name: "Ginger",
    position: "defender",
    isCaptain: false,
  },
  {
    name: "Skipper",
    position: "goalkeeper",
    isCaptain: false,
  }
  ],
}
// declare team,year and headCoach variables
const team = document.getElementById("team");
const year = document.getElementById("year");
const headCoach = document.getElementById("head-coach");

// text content for team, year headCoach variables
team.textContent = footballTeam.team;
year.textContent = footballTeam.year;
headCoach.textContent = footballTeam.headCoach;
//declare player cards variable id 'player-cards'
const playerCards = document.getElementById("player-cards");

footballTeam.players.forEach(player => {
  // create new div/card
    const cardDiv = document.createElement("div");
    //add class 'player-card' to cardDiv
    cardDiv.classList.add("player-card");
    //create new heading and paagraph elements
    const cardHeading = document.createElement("h2");
    const cardPara = document.createElement("p");

    // add text content to new h2 and p elements
    cardHeading.textContent = footballTeam.name;
    cardPara.textContent = player.isCaptain? `(Captain ${player.position})` : player.position;
// add cardHeading and cardPara cardDiv
cardDiv.appendChild(cardHeading);
cardDiv.appendChild(cardPara);
//append cardDiv tp playerCards
playerCards.appendChild(cardDiv);

})

const playerCard = document.getElementsByClassName(".playerCard");


const selectElement = document.getElementById("players");
const selectValue = selectElement.value
console.log(selectElement.length)

 for(element of selectElement){
	console.log(element.value)
}
//console.log(playerCard);
	selectElement.addEventListener("change",() => {
  
for(element of selectedValue){
	console.log(element)
  if(element === "all"){
    playerCards.style.display = " block";
  }else if(element === "forward" && footballTeam.players.position === "forward"){
    playerCard.style.display = "block";
  }else if(element === "midfielder" && footballTeam.players.position === "midfielder"){
    playerCard.style.display = "block";
  }else if(element === "defender" && footballTeam.players.position === "defender"){
    playerCard.style.display = "block" 
  }else if(element === "goalkeeper" && footballTeam.players.position === "goalkeeper"){
    playerCard.style.display = "block"
  }else{
    playerCards.display = "none"
  }
}
});

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15

Challenge Information:

Build a Set of Football Team Cards - Build a Set of Football Team Cards

Please talk about what tests 11-15 are and how you’ve gotten stuck trying to figure out what is wrong with your code.

Hi @dreyes61

Did you check the console?

Uncaught ReferenceError: element is not defined

Happy coding

Actually I don’t get an error. I am able to log the length of the selectElement ,5, and iterate over the selectValue and display the 5 elements,
all
forward
midfielder
defender
goalkeeper
But i can’t pass tests 11 through 15

do not hide the elements filtered out, remove them from the page

Thank s for your help,

Ok so i figured out how to get to the different options of the players select element. But i can’t understand why the cards won;t display properly.
And help would be appreciated.

const footballTeam = {
  team: "Drunken sailors",
  year: 2025,
  headCoach: "Popeye the sailorman",
  players: [
    {
      name: "Skipper",
      position: "forward",
      isCaptain:  true, 
    },
    {
      name: "Gilligan",
      position: "midfielder",
      isCaptain:  false, 
    },
    {
      name: "Mr Howell",
      position: "defender",
      isCaptain:  false, 
    },
    {
      name: "Ginger",
      position: "goalkeeper",
      isCaptain:  false, 
    },
  ],
}
const team = document.getElementById("team").textContent = footballTeam.team;
const year = document.getElementById("year").textContent = footballTeam.year;
const headCoach = document.getElementById("head-coach").textContent = footballTeam.headCoach;


const cardContainer = document.getElementById("player-cards");
const card = document.getElementsByClassName("player-card")

// loop thru team
footballTeam.players.forEach(player => {
	// create div h2 and p elements
	const card = document.createElement("div");
	const cardHeader = document.createElement("h2")
	const cardPara = document.createElement("p");
	// add 'player-card' class to div
	card.classList.add("player-card");
	// add text content to h2 and p elements
	cardHeader.textContent = player.name + " - ";
	cardPara.textContent = player.isCaptain? `(Captain) - Position: ${player.position}` : `Position: ${player.position}`
// append h2 and p elements to div element 
card.appendChild(cardHeader);
card.appendChild(cardPara);
// append playerCard element to playerCards element
cardContainer.appendChild(card);

})

for(let i = 0; i < card.length; i++){

//console.log(card.length)
console.log(card[i].textContent)
}



players.addEventListener("change",function(event){
	const option = event.target.value
	if(option === "all"){
		alert("all")
}else if(option === "forward"){
		alert("forward")
		cardContainer.textContent = card[0].textContent
}else if(option === "midfielder"){
		alert("midfielder")
		cardContainer.textContent === card[1].textContent
}else if(option === "defender"){
		alert("defender")
		cardContainer.textContent = card[2].textContent
}else if(option === "goalkeeper"){
		alert("goalkeeper")
		cardContainer,textContent = card[3].textContent
}

})

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>
      Build a Set of Football Team Cards
    </title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1 class="title">Team stats</h1>
    <main>
      <div class="team-stats">
        <p>Team: <span id="team"></span></p>
        <p>Year: <span id="year"></span></p>
        <p>Head coach: <span id="head-coach"></span></p>
      </div>
      <label class="options-label" for="players">Filter Teammates:</label>
      <select name="players" id="players">
        <option value="all">All Players</option>
        <option value="forward">Position Forward</option>
        <option value="midfielder">Position Midfielder</option>
        <option value="defender">Position Defender</option>
        <option value="goalkeeper">Position Goalkeeper</option>
      </select>
      <div class="cards" id="player-cards"></div>
    </main>
    <footer>&copy; freeCodeCamp</footer>
    <script src="./script.js"></script>
  </body>
</html>

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

:root {
  --dark-grey: #0a0a23;
  --light-grey: #f5f6f7;
  --white: #ffffff;
  --black: #000;
}

body {
  background-color: var(--dark-grey);
  text-align: center;
  padding: 10px;
}

.title,
.options-label,
.team-stats,
footer {
  color: var(--white);
}

.title {
  margin: 1.3rem 0;
}

.team-stats {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  font-size: 1.3rem;
  margin: 1.2rem 0;
}

.options-label {
  font-size: 1.2rem;
}

.cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.player-card {
  background-color: var(--light-grey);
  padding: 1.3rem;
  margin: 1.2rem;
  width: 300px;
  border-radius: 15px;
}

@media (max-width: 768px) {
  .team-stats {
    flex-direction: column;
  }
}

Hi @dreyes61

What happens when you select All Players from the drop down menu?
To see the effect, select one of the other positions first.

Happy coding

Nothing happens when i choose the all option or any of them.
Can you give me a clue as to why nothing happens? I would be grateful for any help.

So i wrapped the forEach and the create elements in an event listener.
So. When i click on the all option the menu displays. But the others aren’t working, can you give me a hint as to why. The html and css is the same as before,

const footballTeam = {
	team: "Drunken Sailors",
	year: 2025,
	headCoach: "Popeye the sailorman",
	players: [
		{
			name: "Skipper",
			position: "forward",
			isCaptain: 	true,
		},
		{
			name: "Gilligan",
			position: "midfielder",
			isCaptain: 	false,
		},
		
		{
			name: "Mr Howell",
			position: "defender",
			isCaptain: false,	
		},
	
		{
			name: "Ginger",
			position: "goalkeeper",
			isCaptain: false,	
		},
],
}
const team = document.getElementById("team").textContent = footballTeam.team;
const year = document.getElementById("year").textContent = footballTeam.year;
const headCoach = document.getElementById("head-coach").textContent = footballTeam.headCoach;

// declare playerCards variable id player-cards
const playerCards = document.getElementById("player-cards");
const playerCard = document.getElementsByClassName("player-card");


players.addEventListener("change",function(event){
// loop thru footballTeam object
const selection = event.target.value;
footballTeam.players.forEach(player =>{
	if(selection === "all"){
	// create div h2 and p elements
const playerCard = document.createElement("div");
const cardHeader = document.createElement("h2"); 
const cardPara = document.createElement("p");

// add 'player-card' class to div
playerCard.classList.add("player-card");

//add text content to h2 and p elements
cardHeader.textContent = player.name;
cardPara.textContent = player.isCaptain? `(Captain) - Position: ${player.position}` : `Position: ${player.position}`

// append h2 and p elemends to div element
playerCard.appendChild(cardHeader);
playerCard.appendChild(cardPara);
// append playerCard to playerCards
playerCards.appendChild(playerCard);

}else if(selection === "forward"){
		playerCards.textContent = playerCard[0].textContent
}else if(selection === "midfielder"){
		playerCards.textContent = playerCard[1].textContent
}else if(selection === "defender"){
		playerCards.textContent === playerCard[2].textContent
}else if(selection === "goalkeeper"){
		playerCards.textContent = playerCard[3].textContent 
}
})
})

for(card of playerCard){
	console.log(card.textContent)

}

Please look at the console. Your current code is throwing a TypeError when you select from the dropdown.

Ok I fixed the console error. It retrieves the entire player cards element but i ca’t figure out how to display just the player card. And help will be appreciated.

const footballTeam = {
  team: "Drunken Sailors",
  year: 2025,
  headCoach: "Popeye the sailor man",
  players: [
    {
      name: "Skipper",
      position: "forward",
      isCaptain: true,
    },
    {
      name: "Gilligan",
      position: "midfielder",
      isCaptain: false,
    },
    {
      name: "Mr Howell",
      position: "defender",
      isCaptain: false,
    },
    {
      name: "Ginger",
      position: "goalkeeper",
      isCaptain: false,
    },

  ],
}
const team = document.getElementById("team").textContent = footballTeam.team;
const year = document.getElementById("year").textContent = footballTeam.year;
const hadCoach = document.getElementById("head-coach").textContent = footballTeam.headCoach;

const playerCards = document.getElementById("player-cards");
const playerCard = document.getElementsByClassName("player-card");

function showCards(char){
PlayerCards.textContent = "";
footballTeam.players.forEach(player => {
  if(char === "all" || player.position === char){
  const playerCard = document.createElement("div");
	const cardHeader = document.createElement("h2");
	const cardPara = document.createElement("p");

  playerCard.classList.add("player-card");
  cardHeader.textContent = player.isCaptain? `(Captain) - ${player.name}` : `${player.name}`
  cardPara.textContent = `Position: ${player.position}`
  playerCard.append(cardHeader,cardPara);
   return playerCards.appendChild(playerCard);
   }else{playedCards.textcontent = "" 
   return playerCards = playerCard[char]}
 })

}

const playerFilter = document.getElementById("players");

playerFilter.addEventListener("change",() => {
  const selectValue = playerFilter.value
  showCards(selectValue)
})
``
I'm having trouble passing test 11 nd 12 even though everything is displaying properly in the preview window,
`html and css remain the same.

what do you want the forEach in showCards to do?

Loop thru the footballTeamobject and create player cards

that’s quite generic, please describe with more details

Your current code is throwing an error in the console when a player position is selected in the dropdown.

Uncaught ReferenceError: PlayerCards is not defined