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

216
Views
Cómo agregar correctamente un botón a un div en javaScript

Estoy creando una aplicación baisc CRUD y tengo problemas con mis botones de eliminar y editar. Puedo agregar el botón Eliminar al contenedor principal pero no puedo agregarlo al comentario dentro del contenedor.

 let submitBtn = document.getElementById("submitBtn"); let commentContainer = document.getElementById("commentsContainer") //Delete Button let deleteBtn = document.createElement("button"); deleteBtn.classList.add("deleteBtn") deleteBtn.innerText = "Delete" submitBtn.addEventListener("click" , function postComment() { let comment = document.getElementById("comment").value let newComment = document.createElement("div") if(comment == ""){ //No comment typed alert alert("Please type a comment!") } else { //Inserting comment into container commentContainer.appendChild(newComment) newComment.classList.add("comment") //Line in question newComment.appendChild(deleteBtn) //commentContainer.appendChild(deleteBtn) works just fine //Setting Comment text newComment.innerText = comment; } deleteBtn.addEventListener("click" ,() =>{ //Deleteing all comment attributes newComment.remove() deleteBtn.remove() }) })

El botón funciona, sin embargo, no puedo entender por qué cuando trato de agregarlo al div que contiene el comentario, JS no crea el botón, pero cuando lo agrego al div principal de comentarios, funciona bien.

Cualquier y toda ayuda es muy apreciada. ¡Gracias!

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

commentContainer.appendChild(newComment)

Debe ser la última instrucción y debe moverse al final de else debajo de esta línea:

 newComment.innerText = comment;
about 4 years ago · Santiago Gelvez 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!