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

219
Views
¿Cómo puedo crear una línea de espacio (como un br) cuando el usuario hace clic en el botón?

Necesito una función de javascript o cualquier cosa, así que cuando el usuario pone nombres en el texto de tipo de entrada, pone el texto en un párrafo debajo de #final y así sucesivamente agregando un espacio cada vez que el usuario hace clic en el botón.

 function ir() { let nombre = document.getElementById("nombre").value; let monto = document.getElementById("monto").value; let total = document.getElementById("total"); let final = document.getElementById("final"); let aporte = document.getElementById("aporte"); for (i = 0; i < monto; i++) { total.innerText = ` ${nombre} : ${monto}`; }; if (total > 0) { monto + monto } final.innerHTML = `${monto}`; aporte.innerHTML = `${monto}`; };
 <html> <p>Nombre</p> <input type="text" id="nombre"> <br> <p>Monto</p> <input type="number" id="monto"> <br> <button onclick="ir()">Enviar</button> <br> <p>Total: <span id="final"></span> </p> <div id="total"> </div> <p>A cada uno le toca aportar: <span id="aporte"></span></p> </div> </html>

¿Cómo puedo hacerlo?

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

0

Bienvenido a StackOverflow,

Prueba esta función:

 function ir() { // Set everything to const because they don't change and it's good practise const nombre = document.getElementById("nombre").value; // Monto changes below, so we can use LET!! let monto = document.getElementById("monto").value; const total = document.getElementById("total"); const final = document.getElementById("final"); const aporte = document.getElementById("aporte"); // Create new element under total const lineBreak = document.createElement("br"); let newTotal = document.createTextNode(` ${nombre} : ${monto} `); total.appendChild(lineBreak); total.appendChild(newTotal); // Set monto to monto + the value already there // Use Number to make it number (without Number it's string, 1+2 = 12 and not 3) monto = Number(monto) + Number(final.innerHTML); final.innerHTML = `${monto}`; aporte.innerHTML = `${monto}`; };

Lo que hace es crear un nuevo elemento debajo del total y actualizarlo cada vez que el usuario hace clic en el botón.

El total también se actualiza también...

¿Es esto lo que querías? Espero haber ayudado :)

about 4 years ago · Juan Pablo Isaza Report

0

Una forma potencial de hacer esto sería verificar los valores de nombre y monto . Si están vacíos (o solo espacios), podemos crear y agregar un elemento <br>.

 if (monto.trim() == "" || nombre.trim() == "") { let lineBreak = document.createElement("BR"); total.appendChild(lineBreak);

No estoy exactamente seguro de a qué elemento desea agregar esto, así que cámbielo en consecuencia a element.appendChild(createdElementName) .

¡Espero haber podido ayudar!

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!