Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
How does the querySelector work on elements which are created dynamically
const element = document.createElement("article");
  // add classes
  element.classList.add("grocery-item");

  const attr = document.createAttribute("data-id");
  attr.value = id;
  element.setAttributeNode(attr);
  element.innerHTML = `<p class="title">${value}</p>
            <div class="btn-container">
              <button type="button" class="edit-btn"><i class="fas fa-edit"></i></button>
              <button type="button" class="delete-btn"><i class="fas fa-trash"></i></button>
            </div>`;
  // append child
  list.appendChild(element);
  // Edit Button
  const editBtn = element.querySelector(".edit-btn");

I am making a grocery list website and each item in the list like the milk and eggs come under their own article element which is being created in the javascript and I have added the code for that too. Now in the screenshot that I have posted, I have added 3 items in the grocery list, so there are 3 article elements created as per code given under which comes the edit and delete buttons. When I click on the edit button for,suppose eggs,how does it know that I have clicked on the eggs's edit button?

const editBtn = element.querySelector(".edit-btn");

This line accesses the edit button within the required article element but how does it access the required article element in the first place? There is no querySelector for selecting all the article elements in the rest of the code.

I am not entirely sure how to ask this question, so please do tell me some improvements

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda