How can I target all elements with same ID or class name at once?

So, I’m just messing around and seeing how far I can get into making a “memory game” All I’m trying to do at the moment is have it to where when I hit the start game button, all the images display will change to “none” after 3 seconds have passed. so, it’ll be just empty cards with no image

Now when I do this with ID’s it only targets the first image and the rest still stay on display “block” however it does at least work on that first image. I’m guessing it maybe only targets the first one because you can’t have the same ID on more than one element?

BUT

when I try to do it by class name it won’t even let me change the style/display at all.

What is the easiest way of doing this? I’m stumped

<html>
    <head>
         <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=MedievalSharp&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="index.css">
    </head>
    <body>
        <div class="grid-container">
            <div class="grid-item"><img src="blastoise.png" alt="" id="image"></div>
            <div class="grid-item"><img src="charizard.png" alt="" id="image"></div>
            <div class="grid-item"><img src="venosaur.png" alt="" id="image"></div>
            <div class="grid-item"><img src="electabuzz.png" alt="" id="image"></div>
            <div class="grid-item"><img src="voltorb.webp" alt="" id="image"></div>
            <div class="grid-item"><img src="raichu.webp" alt="" id="image"></div>
            <div class="grid-item"><img src="pika.png" alt="" id="image"></div>
            <div class="grid-item"><img src="muk.webp" alt="" id="image"></div>
            <div class="grid-item"><img src="arbok.png" alt="" id="image"></div>
        </div>   
        
        <button id="start-button">Start Game</button>
        <script src="index.js"></script>
    </body>
</html>
function startGame() {
    document.getElementById('image').style.display = 'none'
}

document.getElementById('start-button').addEventListener('click', function() {
    setTimeout(startGame, 3000)
})
.grid-container {
    display: grid;
    grid-template-columns: auto auto auto;
    background-color: #2196F3;
    padding: 10px;
  }
  .grid-item {
    background-color: rgba(255, 255, 255, 0.8);
    border: 1px solid rgba(0, 0, 0, 0.8);
    padding: 20px;
    font-size: 30px;
    text-align: center;
    width: 150px;
    height: 100px;
  }
  #image {
    width: 100px;
    display: block;
  }

#start-button {
    padding: 10px;
    position: absolute;
    left: 41%;
}

First of all, ids are supposed to be unique to the page they are on, so get rid of id="image".

To target all elements with the the same class name, you can read about the querySelectorAll method. It will return an array-like object that you can iterate through and perform the style change to the elements in it.

The name of the method also gives it away getElementById it is Element not Elements.

Even when you use querySelectorAll with an id selector it will just return the first element with that id. As said, you can use a class or an element type selector with querySelectorAll.

Thanks, I will look into it :slight_smile:

I can’t for the life of me figure this out haha.

I’ve tried for loops, and I’ve tried mapping. Every time I try to apply .style.display = onto anything it doesn’t work.

Show us your latest attempt.

let gridContainer = document.getElementById('grid-container')


function startGame() {
    let list = document.querySelectorAll('.image')
    for ( i = 0; i < list.length; i++ ) {
            list[i].style.display ='none'       
    }  
}
startGame()

document.getElementById('start-button').addEventListener('click', function() {
    setTimeout(startGame, 3000)

})

I mean I’m just trying anything at this point. I’m guessing you can’t just target an entire array with .style

It doesn’t seem to let me use .style.display on anything no matter what I do

EDIT* I’m reading that you can’t use .style on arrays at all. So I’m trying to come up with another solution

Did you change your HTML or is it still the same as you originally posted above?

I have left it the same. EDIT* No I didn’t haha

Explain what you think this does.

I thought it grabbed all of the elements from my HTML that were specified. So, in this example it would bring in everything with the class of “image”

pretty similar to getElementByClassName but it seems to be more flexible as you can grab by ID with it too?

It would, but the HTML you posted above does not have any elements with a class of image.

I’m so sorry yes, the HTML is updated!

I didn’t realize the one above had ID of image

Please post your HTML.

Also, I am not sure I understand why you are displaying all the images and then setting their display to none. Why not just start with all of them not being displayed when the app loads (using CSS)?

Having the images show and then disappear is kind of distracting from a user experience point of view.

<html>
    <head>
         <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=MedievalSharp&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="grid-container">
            <div class="grid-item"><img src="blastoise.png" alt="" class="image"></div>
            <div class="grid-item"><img src="charizard.png" alt="" class="image"></div>
            <div class="grid-item"><img src="venosaur.png" alt="" class="image"></div>
            <div class="grid-item"><img src="electabuzz.png" alt="" class="image"></div>
            <div class="grid-item"><img src="voltorb.webp" alt="" class="image"></div>
            <div class="grid-item"><img src="raichu.webp" alt="" class="image"></div>
            <div class="grid-item"><img src="pika.png" alt="" class="image"></div>
            <div class="grid-item"><img src="muk.webp" alt="" class="image"></div>
            <div class="grid-item"><img src="arbok.png" alt="" class="image"></div>
        </div>   
        
        <button id="start-button">Start Game</button>
        <script src="index.js"></script>
    </body>
</html>

I technically got it to work but I’m not happy with it. I don’t like that I have a giant block of code in my JS but I’m sure I’ll find a way to improve it!
I simply made to where the function changed the class from image to a new ID that displays “none”. It’s not my best work but it’ll do for now haha.

let gridContainer = document.getElementById('grid-container')


function startGame() {
            gridContainer.innerHTML =  `
            <div class="grid-item"><img src="blastoise.png" alt="" id="new-class"></div>
            <div class="grid-item"><img src="charizard.png" alt="" id="new-class"></div>
            <div class="grid-item"><img src="venosaur.png" alt="" id="new-class"></div>
            <div class="grid-item"><img src="electabuzz.png" alt="" id="new-class"></div>
            <div class="grid-item"><img src="voltorb.webp" alt="" id="new-class"></div>
            <div class="grid-item"><img src="raichu.webp" alt="" id="new-class"></div>
            <div class="grid-item"><img src="pika.png" alt="" id="new-class"></div>
            <div class="grid-item"><img src="muk.webp" alt="" id="new-class"></div>
            <div class="grid-item"><img src="arbok.png" alt="" id="new-class"></div>` 
}


document.getElementById('start-button').addEventListener('click', function() {
    setTimeout(startGame, 3000)

})
id="new-class"

That is just confusing and you still can’t use the same id value for more than one element. I mean you can, but it is wrong.

I ended up getting everything to work in a way that I’m prouder of!
it’s a little clunky but it is doing what I want it to do.

and also replying to Randel, the reason I’m having it start with display block and taking off the display 3seconds after game start is because I want them to see the images before the game starts and then they’re going to have to remember where they were once, they are taken away. I’m honestly just messing around and trying to get used to things I’m learning. I don’t like continuing to learn new things unless I can really grasp what I’ve learned.

I don’t like the fact that I’m guessing half the time trying to make things work. I want to have a solid understanding of why I’m doing what I’m doing. Not just guessing and crossing my fingers Haha.

I’m sure this is still an eye sore to look at for you guys but I’m making baby steps :slight_smile:

let gridContainer = document.getElementById('grid-container')

let pokemonArray = ["blastoise.png", "charizard.png", "venosaur.png", "electabuzz.png", "voltorb.webp", "raichu.webp", "pika.png", "muk.webp", "arbok.png"]

function startGame() {
      gridContainer.innerHTML = ''
    for ( i = 0; i < pokemonArray.length; i++ ) {        
        newPokemon = ` <div class="grid-item"><img src="${pokemonArray[i]}" alt="" id="new-class"></div> ` 
        gridContainer.innerHTML += newPokemon        
    }   
     
}
 

document.getElementById('start-button').addEventListener('click', function() {
    setTimeout(startGame, 3000)

})

My point is, calling an id new-class is confusing. The name doesn’t explain what it is and calling it a class when it is applied to an id is at best confusing.

Also, an id, as already explained, is supposed to be unique. You should not give the same id to more than one element.

I wasn’t really paying attention to names; I was just throwing stuff around trying to get it to work.

sometimes when I’m really just trying to get things to work my code gets sloppy and then I go back and fix it once I’ve gotten things settled. I know it’s a bad habit and I should stop .