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

163
Views
JavaScript / HTML funciona con DOM, botón que cuenta clic y crea una nueva cadena cada vez que hago clic

Necesito crear un botón que cuente el clic pero haga cada vez una nueva cadena. Tengo esa función con alcance que cuenta cada vez que hago clic, pero no puedo entender, por qué en HTML cada vez que cuenta cero, debería ser así: (Verticalmente, cada clic crea una nueva cadena con el recuento actualizado) 1 2 3 4 . ..

 <form action="#"> <input type="button" value="Count" onclick="add_count()"> </form> function add_count() { let integer = (function () { let counter = 0; return function() {return counter++;}; }()); let tag = document. createElement("p"); let text; text = document. createTextNode(integer()); tag. appendChild(text); let element = document. getElementsByTagName("body")[0]; element. appendChild(tag)};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solo necesita mover la inicialización del counter fuera del alcance de la función

He modificado un poco tu código.

Nota: intente no usar detectores de eventos en línea, en su lugar use detectores de eventos en el archivo JavaScript

 let counter = 0; const button = document.querySelector('input'); function add_count() { let integer = (() => counter++); const tag = document.createElement("p"); const text = document.createTextNode(integer()); tag.appendChild(text); const element = document.body element.appendChild(tag) }; button.addEventListener('click', add_count)
 <form action="#"> <input type="button" value="Count"> </form>

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!