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

107
Views
Agregar elemento a la lista al presionar una tecla y borrar la lista

Como en el tema, mi trabajo es hacer un código en el que el usuario escribe un texto en el cuadro de texto y cuando el usuario presiona la tecla Intro, el texto del cuadro de texto debe agregarse a la lista ul en el encabezado Cosas. También agregué el botón de reinicio, pero no funciona porque agregar elementos a la lista no funciona, pero creo que debería funcionar cuando lo arregle. No se donde me equivoque. Alguien podría ayudarme o aconsejarme que debo hacer?

 <!DOCTYPE html> <html> <head> <title>List</title> </head> <body> <h1 id="title">List</h1> <form> <input type="text" id="user-todo" placeholder="List" required> </form> <h2 id="todo-header">Things</h2> <ul id="list"> </ul> <button id="clear">Reset</button> <script> var input = document.getElementById("user-todo"); input.addEventListener("keyup", function(event) { if (event.keyCode === 13) { event.preventDefault(); // what happens when user hits ENTER $(document).ready(function() { $('ul').append("<li>"+($('#user-todo').val()) + "</li>"); }); } }); function clear() { document.getElementById("list").innerHTML = ''; } </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Parece que estás en un curso obsoleto, puedes aprender Modern JS en:

https://exploringjs.com/impatient-js/

https://phoenix35.js.org/

Prescindiendo del uso de JQuery, un marco que ya no es necesario en estos días, obtienes:

 const input = document.querySelector("#user-todo"); const clear = document.querySelector("#clear"); const list = document.querySelector("#list"); input.addEventListener("keyup", function(event) { if (event.keyCode === 13) { const li = document.createElement("li"); li.textContent = input.value; input.value = ""; list.appendChild(li); } }); clear.addEventListener("click", function() { list.innerHTML = ""; });
 <!DOCTYPE html> <html> <head> <title>List</title> </head> <body> <h1 id="title">List</h1> <form onsubmit="return false;"> <input type="text" id="user-todo" placeholder="List" required> </form> <h2 id="todo-header">Things</h2> <ul id="list"></ul> <button id="clear">Reset</button> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

Si cambia esta línea, debería funcionar. https://codesandbox.io/s/exciting-sinoussi-ckw8b?file=/index.html

 input.addEventListener("keydown", function (event) {
about 4 years ago · Juan Pablo Isaza Report

0

 <!DOCTYPE html> <html> <head> <title>List</title> </head> <body> <h1 id="title">List</h1> <input type="text" id="user-todo" placeholder="List" required> <h2 id="todo-header">Things</h2> <ul id="list"> </ul> <button onclick="clearFunction()">Reset</button> <script> function clearFunction() { document.getElementById("list").innerHTML = " "; } var input = document.getElementById("user-todo") var list = document.getElementById("list"); input.addEventListener("keyup", function (event) { if (event.keyCode === 13) { event.preventDefault(); var input_val = document.getElementById("user-todo").value; let li = document.createElement("li"); li.innerText = input_val; list.append(li); } }); </script> </body>

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!