Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Cómo obtener una clase de contenido agregado dinámicamente a través de innerhtml

Estoy agregando contenido a mi HTML a través de JS usando innerHTML. Pero ahora, cuando intento acceder a una clase adjunta a ese contenido, obtengo un valor nulo. ¿Cómo accedería a la clase?

 fetch(url) .then((response) => { return response.json(); }).then(data => { for (var i=0; i < data['data'].length; i++){ var attributes = data.data[i].attributes, title = attributes.title, descr = attributes.description, author = attributes.author, available = attributes.available, year = attributes.year, id = data.data[i].id; var div = document.createElement("div"); div.innerHTML = `title = ${title}, descr = ${descr}, author = ${author}, available = ${available}, year = ${year} <button type="button" value="${id}" class="delete">delete</button>` document.querySelector('.movieList').appendChild(div) } }); const btns = document.querySelectorAll(".delete"); console.log(btns);

Probé .getElementByClassName, pero tampoco funciona :/

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Es un problema de tiempo. Debe llamar a querySelectorAll después de agregar los elementos.

Intente poner const btns = document.querySelectorAll(".delete"); justo después de document.querySelector('.movieList').appendChild(div) .

Actualmente, en el momento en que intenta encontrar los botones, todavía no están allí.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!