1

i'm having a trouble using the id from a template string item

const elementoParaInserirJogosNaLista = document.getElementById("listaJogos");

function exibirJogosNaTela(listaDeJogos) {
  elementoParaInserirJogosNaLista.innerHTML = "";
  listaDeJogos.forEach((jogo) => {
    elementoParaInserirJogosNaLista.innerHTML += `
        <div class="jogo">
        <a href="paginajogo.html">
        <img class="jogo__imagem" src="${jogo.imagem}" alt="${jogo.titulo}" />
        </a>
        <h2 class="jogo__titulo">${jogo.titulo}</h2>
        <p class="jogo__preco" id="preco">R$${jogo.preco}<a ><img class="jogo__carrinho" id="addCarrinho" src="./images/addcart.png" alt="Adicionar ao carrinho"/></p><a/>
        
        </div>
        `;
  });
}

i've tried to use the id "addCarrinho" and nothing happens

i'm newb on developing

const botoesAddCarrinho = [];
botoesAddCarrinho = document.querySelectorAll(".jogo__carrinho");
botoesAddCarrinho.forEach((evento) =>
  evento.addEventListener("click", addNoCarrinho)
);

function addNoCarrinho () {
  console.log('ok')
}

i've changed the selector to by the class, but nothings happens, is like the nothing was selected

i'm using the exibirNaTela on the fetch with the json

let jogos = [];
const endpointDaAPI ="jogos.json"

getBuscarJogosDaAPI();

async function getBuscarJogosDaAPI() {
  const respost = await fetch(endpointDaAPI);
  jogos = await respost.json();
  exibirJogosNaTela(jogos.jogos);
  
}
  • 1
    `document.querySelectorAll("#addCarrinho")` makes no sense. An ID belongs to a unique element. There are no two elements with the same ID within the same document, unless you have invalid HTML. [Validate your HTML](//validator.nu). You cannot reassign a `const` variable anyway. What is the point of `botoesAddCarrinho = []`? This array is never used. `.addEventListener("click", console.log("ok"))` makes no sense. [`addEventListener`](//developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener) expects a function as the second argument. `console.log("ok")` isn’t a function. – Sebastian Simon Feb 10 '23 at 03:56
  • Read the documentation: [`id`](//developer.mozilla.org/en/docs/Web/HTML/Global_attributes/id). See [What is the difference between a function call and function reference?](/q/15886272/4642212). Use [event delegation](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of adding several event listeners — it’s more maintainable and applies to dynamically added elements. See [the tag info](/tags/event-delegation/info) and [this Q&A](/a/34896387/4642212). – Sebastian Simon Feb 10 '23 at 03:58
  • @SebastianSimon maybe if you have 20 elements with `id='foo'` then `querySelectorAll('#foo')` will return those 20 – Tachibana Shin Feb 10 '23 at 03:59
  • @TachibanaShin Yes, but that doesn’t mean that’s the correct thing to do. – Sebastian Simon Feb 10 '23 at 04:00
  • where are you calling the `exibirJogosNaTela` function? this has to do with javascript's synchronicity – Tachibana Shin Feb 10 '23 at 04:01
  • Ok, so i need to use the class to select all the itens ? – Vinicius V. Rodrigues Feb 10 '23 at 04:01
  • @SebastianSimon there's nothing wrong with google having 1000 elements with the same id on their youtube page – Tachibana Shin Feb 10 '23 at 04:02
  • @ViniciusV.Rodrigues please answer my question – Tachibana Shin Feb 10 '23 at 04:05
  • i'm using it on the fetch with the json – Vinicius V. Rodrigues Feb 10 '23 at 04:09

0 Answers0