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

672
Views
Intentando poner onclick en el botón que se agregó a DOM por otro botón pero obteniendo el error Uncaught TypeError que se trata de configurar 'onclick'

Este es mi código HTML, es simple, solo un bloque div con botón.

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Button Problem</title> <style> </style> </head> <body> <div class="users"> <button value ="0" id="button1">CLICK ME</button> </div> <script src="./script.js"></script> </body> </html>

y aquí está mi código JavaScript:

 document.getElementById("button1").onclick = function () { let el = document.querySelector(".users"); el.innerHTML += `<button id="button2">LEGIT BUTTON</button>`; } document.getElementById("button2").onclick = function(){ console.log("button2 is working!!!"); }

entonces, en el código HTML, como puede ver, tengo un botón en el bloque div. en JavaScript pongo onclick en ese botón y después de hacer clic en ese botón se agrega un nuevo botón en DOM. pero recibo el error "Error de tipo no detectado: no se pueden establecer las propiedades de nulo (configurando 'onclick') en" Quiero que el segundo clic espere a que ocurra el primer clic para agregar un nuevo botón. ¿Qué tengo que hacer?

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

0

debe llamar a document.getElementById("button2").onclick después de agregar este nuevo button2 al DOM

about 4 years ago · Juan Pablo Isaza Report

0

Según ese código, está intentando agregar un detector de eventos al botón 2 antes de que se agregue al dom. Intente moverlo así para asegurarse de que exista primero:

 document.getElementById("button1").onclick = function () { let el = document.querySelector(".users"); el.innerHTML += `<button id="button2">LEGIT BUTTON</button>`; // it will now exist and you can add a listener document.getElementById("button2").onclick = function(){ console.log("button2 is working!!!"); } }
about 4 years ago · Juan Pablo Isaza Report

0

Sería más claro crear el elemento dom y agregarle el oyente directamente después de crearlo:

 document.getElementById("button1").addEventListener("click", function () { let el = document.querySelector(".users"); // Create the element using the API let btn = document.createElement("button"); // Add element details (you don't need the ID anymore // because we'll use the javascript DOM element directly) btn.setAttribute("id", "button2"); btn.innerText = "LEGIT BUTTON"; // Add event listener btn.addEventListener("click", function(){ console.log("button2 is working!!!"); }); // Append the new button to the element el.appendChild(btn); });
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!